Selection sort
A sorting algorithm that each round finds the smallest of what's left and puts it at the end of the sorted part. Always O(n²) and unstable.
Example
[4, 1, 3] → finds 1 and swaps it with 4: [1, 4, 3] → finds 3: [1, 3, 4].
Test yourself: Selection sort
Selection sort
After selection sort's first round, index 0 holds the ___ element of all.
In [5, 2, 8, 1], the 1st round finds 1 and swaps it with 5: [1, 2, 8, 5]. The 1 never moves again.
Selection sort
In [7, 3, 9, 1], after selection sort's first round the array is [1, 3, 9, ___].
The smallest is 1, at index 3; it's swapped with the 7 at index 0. The 7 ends up where the 1 was.
Selection sort
Selection sort with 1,000 elements does about ___ comparisons.
999 + 998 + ... + 1 = 499,500. With 10,000 elements it'd be about 50 million.
0/3
▶ Play