Selection Sort Visualization
Selection Sort finds the smallest remaining value and swaps it to the front, again and again.
How it works
Selection Sort scans the unsorted part of the list to find its smallest value, then swaps it into the first unsorted position. The sorted part grows by one each pass.
It always does about n²/2 comparisons, even on sorted input, so it is one of the slower sorts here. Its one strength is that it makes at most n − 1 swaps, which matters when writing to memory is expensive. It is not stable, because the swap can jump a value over others equal to it.
What to watch for
Watch the long scan sweep across the unsorted part every pass, then one value jumps to the front of it.
Complexity
- Best
- n²
- Average
- n²
- Worst
- n²
- Extra space
- 1
- Stable
- No
Race it
Same shuffle, same speed, one winner: Selection vs Bubble. Or open the studio to try other layouts, sizes and speeds, and make a video of it.
FAQ
Is Selection Sort stable?
Not in its usual form: the long-distance swap can change the order of equal values.
Selection Sort vs Bubble Sort: which is faster?
Both are O(n²). Selection Sort does far fewer swaps; Bubble Sort can stop early on nearly sorted data. Race them to see on the same shuffle.