Usage: Perform selection sort for a list of integers. Click the Next button to
find the smallest element (highlighted in red) and swap this element with the first
element (highlighted in orange) in the the unsorted sublist. The elements that are
already sorted are highlighted in light green.
Click the Reset button to start over with a new random list of the specified size (min 3 and max 20). The Custom Input button enables you to enter a custom list.
i: 1
↓
i
currentMin
currentMinIndex
A list is filled with random numbers.
1 void selectionSort(double list[], int arraySize)
2 {
3 for (int i = 0; i < arraySize - 1; i++)
4 {
5 // Find the minimum in the list[i..arraySize-1]
6 double currentMin = list[i];
7 int currentMinIndex = i;
8
9 for (int j = i + 1; j < arraySize; j++)
10 {
11 if (currentMin > list[j])
12 {
13 currentMin = list[j];
14 currentMinIndex = j;
15 }
16 }
17
18 // Swap list[i] with list[currentMinIndex] if necessary;
19 if (currentMinIndex != i)
20 {
21 list[currentMinIndex] = list[i];
22 list[i] = currentMin;
23 }
24 }
25 }