Visual C# Guide
Selection Sort
The selection sort works by looking at the first item in the array. It compares this with all of the other elements in the array and swaps it with the smallest item in the remainder of the array. Then the second item is examined in the same way. This process continues with each element except the last.
The following algorithm can be used,
For outerLoop = 0 To arrayLength - 2
minValue = outerLoop
For innerLoop = outerLoop + 1 To arrayLength - 1
If item[innerLoop] < item[minValue] Then minValue = innerLoop
Next innerLoop
temp = item[outerLoop]
item[outerLoop] = item[minValue]
item[minValue] = temp
Next outerLoop
Selection Sort Procedure
The following procedure can be used to sort the array that we have created to implement the other algorithms,
static void SelectionSort()
{
int min;
double temp;
for (int outer = 0; outer < randomArray.GetLength(0) - 1; outer++)
{
min = outer;
for (int inner = outer + 1; inner < randomArray.GetLength(0); inner++)
{
if (randomArray[inner] < randomArray[min])
{
min = inner;
}
}
temp = randomArray[outer];
randomArray[outer] = randomArray[min];
randomArray[min] = temp;
}
}
Having A Go
- Add this little procedure to your sorting program and compare the execution time of all 3 algorithms with 100, 1000 and 10000 items.