Selection Sort
Selection sort is a sorting algorithm. It works by selecting the smallest element of the array and placing it at the head of the array. Then the process is repeated for the remainder of the array, the next largest element is selected and put into the next slot, and so on down the line.
void SelectionSort(int* data, int count) {
int min;
for (int i = 0; i < count - 1; ++i)
{
min = i;
for (int j = i + 1; j < count; ++j)
{
if (data[j] < data[min])
{
min = j;
}
}
int temp = data[min];
data[min] = data[i];
data[i] = temp;
}
}
Example
int data[] = { -1, 25, -58964, 8547, -119, 0, 78596 };
SelectionSort(data, 7);
Output
-58964
-119
-1
0
25
8547
78596