Selection Sort Visualization
Find the smallest remaining value, put it where it belongs, repeat.
Selection sort is the algorithm you’d invent if someone handed you a messy deck of cards and asked you to sort it methodically: scan everything, pull out the smallest card, put it first. Scan what’s left, pull out the next smallest, put it second. Repeat until nothing is left to scan. In the animation below you’ll see exactly that rhythm: a long scanning sweep of comparisons, then a single decisive swap, then a bar turning green on the left edge.
How it works
The array is split into a sorted prefix (the green bars growing from the left) and an unsorted remainder. On each iteration the algorithm walks the entire unsorted region tracking the index of the minimum value. That’s the long run of comparison highlights you see between swaps. When the scan finishes, the minimum is swapped into the first unsorted slot and that position is marked sorted, permanently.
Notice how different the animation feels from bubble sort: bubble sort swaps constantly, while selection sort makes at most one swap per pass, at most n − 1 swaps total, the fewest of any comparison sort here. The comparisons, though, are relentless: the scan length shrinks by only one each round, so the total is always about n²/2 regardless of the input. Hand it a fully sorted array and it will still inspect every pair, which is why best, average, and worst case are all O(n²), with no early exit possible.
When interviewers ask about it
Selection sort earns its place in interviews through two sharp follow-up questions. First: why isn’t it stable? That long-distance swap can jump an element clear over an equal neighbor, reordering duplicates (a subtle point that shows you actually understand what stability means). Second: when would minimizing swaps matter? If writes are dramatically more expensive than reads (think flash memory wear, or huge records where moving data is costly), selection sort’s O(n) writes are a genuine advantage over bubble sort’s O(n²). Being able to name a scenario where a “bad” algorithm wins is exactly the kind of trade-off reasoning interviewers probe for.
It’s also the stepping stone to heapsort: replace the linear “scan for the minimum” with a heap that hands you the extreme element in O(log n), and selection sort becomes heap sort: same idea, better data structure.
Selection sort is covered alongside bubble sort in Day 5: Multi-Dimensional Arrays and Sorting, with the complexity toolkit from Day 3: Introduction to Time Complexity.
Complexity at a glance
| Best | Average | Worst | Space | Stable |
|---|---|---|---|---|
| O(n²) | O(n²) | O(n²) | O(1) | No |
The exact code you are watching
This is the real generator that drives the animation above, read from
sort-algorithms.js at build time, so the code and the
visualization can never drift apart. Each yield is one
visual step.
1function* selectionSort(a) {
2 for (let i = 0; i < a.length - 1; i += 1) {
3 let min = i;
4 for (let j = i + 1; j < a.length; j += 1) {
5 yield { type: 'compare', i: min, j: j };
6 if (a[j] < a[min]) {
7 min = j;
8 }
9 }
10 if (min !== i) {
11 const tmp = a[i];
12 a[i] = a[min];
13 a[min] = tmp;
14 yield { type: 'swap', i: i, j: min };
15 }
16 yield { type: 'markSorted', i: i };
17 }
18 yield { type: 'markSorted', i: a.length - 1 };
19}