Playground / Selection Sort

Find the smallest, again and again

Selection Sort

Interactive lab

Try it: Selection Sort

How selection sort repeatedly finds the smallest (or largest) remaining value and swaps it into the next position.

How it works

  1. Scan the unsorted part and remember the position of the smallest value.
  2. Swap it into the first unsorted position.
  3. That position is now final; repeat on the rest.
  4. Always n(n−1)/2 comparisons, at most n−1 swaps; not stable.

Default run (27 steps): Selection sort, ascending: [8, 3, 5, 1, 9, 2]. … Sorted ascending: [1, 2, 3, 5, 8, 9] — 15 comparisons, 5 swaps.

Simplified: Up to 12 numbers from 0 to 99. The comparison table runs all five algorithms on the same input. Python's own sort() uses Timsort.

Educational simulation

Loading the simulation…