Selection Sorting
Selection sorting is an algorithm for sorting lists of data and is specifically an in place comparison sort. Generally it is inefficient in large lists and is not faster then an insertion sort on such a list. However, it outperforms (usually) bubble sorting on a relatively small list. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations. The steps are as follows:
1. Find the minimum value in the list
2. Swap it with the value in the first position
3. Repeat the steps above for remainder of the list (starting at the second position)
We again essentially divide the list into two halves (this comes up a lot doesn't it?) Selection sort can also be used on list structures that make adding and removing efficient, such as a linked list. In this case it's more common to remove the minimum element from the remainder of the list, and then insert it at the end of the values sorted so far (i.e. insertion sort).
Let's run through some example code. In this mini program we will initialize an array of 10 elements with random integers ranging between 1 and 100. We will then display the list for debugging purposes and then display it again after the sort has been performed:
srand(time(0));
int arrayOne[10];
for (int i = 0; i < 10; i++)
{
arrayOne[i] = rand() % 100 + 1; // generate random values to sort
}
//display values prior to sorting
for (int i = 0; i < 10; i++)
{
cout << " " << arrayOne[i];
}
void SelectionSort (int[], int);
void SelectionSort(int A[], int length)
{
int i, j, min, minat;
for(i = 0; i<(length-1); i++)
{
minat = i;
min = A[i];
for(j = i+1;j < length; j++) //select the min of the rest of array
{
if(min > A[j]) //ascending order for descending reverse
{
minat = j; //the position of the min element
min = A[j];
}
}
int temp = A[i];
A[i] = A[minat]; //swap
A[minat]=temp;
}
}
#include <iostream>
#include <time.h>
using namespace std;
void SelectionSort (int[], int);
int main()
{
srand(time(0));
int arrayOne[10];
for (int i = 0; i < 10; i++)
{
arrayOne[i] = rand() % 100 + 1; // generate random values to sort
}
//display values prior to sorting
for (int i = 0; i < 10; i++)
{
cout << " " << arrayOne[i];
}
SelectionSort(arrayOne, 10);
cout << endl;
//display values after sorting
for (int i = 0; i < 10; i++)
{
cout << " " << arrayOne[i];
}
cout << endl;
return 0;
}
void SelectionSort(int A[], int length)
{
int i, j, min, minat;
for(i = 0; i<(length-1); i++)
{
minat = i;
min = A[i];
for(j = i+1;j < length; j++) //select the min of the rest of array
{
if(min > A[j]) //ascending order for descending reverse
{
minat = j; //the position of the min element
min = A[j];
}
}
int temp = A[i];
A[i] = A[minat]; //swap
A[minat]=temp;
}
}
Take a look at http://simpleprogrammingtutorials.com/tutorials/sorts/selection-sort.php for illustrations and Java implementation.
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)