Developer Toolbox

Selection sort

Finds the smallest remaining value and swaps it into the next place. It makes very few swaps, but always the same number of comparisons.

  • Ω(n²)
  • Θ(n²)
  • O(n²)
  • O(1)
  • no
  • yes
  • comparing
  • moving
  • current minimum
  • in final place
0 / 399 steps

Comparisons: 0 · pairs compared: 0 of 276 compared (darker = more often) being compared

Comparisons Swaps

Press play: the lines show how the cost grows as the sort runs

Press play or step through the algorithm.

Space: play or pause. Left and right arrows: step. Home and End: jump.

Try this: Pick a reversed list. The number of comparisons stays exactly the same as for any other list.

How it works

Look through the unsorted part and find the smallest value. Swap it with the first unsorted value, so one more value is in its final place. It always checks every remaining value, even when the list is already sorted.

When it is a good choice

Useful when moving data is expensive but reading it is cheap, because it swaps at most once per position. In most other cases insertion sort is the better simple choice.