Skip to content
StackDecode
SortingAnimated

Selection Sort

Repeatedly select the minimum and put it first.

Time

O(n²)

Extra space

O(1)

Use it for

When writes are expensive (at most n swaps).

How it works

  1. Scan the unsorted part to find the smallest value.
  2. Swap it into the first unsorted position.
  3. The sorted part grows by one; repeat.
  4. Always about n²/2 comparisons, but at most n swaps.

Watch it run

Loading animation…