Algorithms / Selection sort

Selection sort

Sort six numbers by repeatedly picking out the smallest one left and putting it next in line, then count exactly how much work that took.

Beginner7 steps~6 min
Sorted part — final positions, never touched again
Unsorted part — still to be scanned
Smallest — the minimum found by the scan
Scanner — walks the unsorted part once per pass
// THE SHORT VERSION

In 30 seconds

Selection sort keeps a sorted part on the left and an unsorted part on the right. Each pass scans the unsorted part for its smallest item and swaps it to the front of that part.

It is simple and makes very few swaps, at most one per pass. But the scanning adds up to about n²/2 comparisons, so it is slow on large inputs, whatever order they start in.

It is also not stable: a swap can move an item past another one of equal value.

Selection sort, step by step

STEPS01 / 07

This is the usual swapping version. A variant that shifts items along instead of swapping is stable, but it gives up the low number of writes.

STEP 01 / 07 An unsorted row
THE MATH
row = 7 4 8 2 6 3 n = 6 items
  1. Step 1: An unsorted row. Six numbers in no particular order. Selection sort splits the row into a sorted part on the left, empty for now, and an unsorted part on the right, and grows the first one item at a time.
  2. Step 2: Scan for the smallest. The scanner walks the unsorted part once and remembers the smallest value seen so far. It starts with 7, replaces it with 4, then with 2. Every other item is compared once, so six items cost five comparisons.
  3. Step 3: Swap it to the front. The 2 trades places with whatever sits at the front of the unsorted part, here the 7. One swap, and 2 is in its final position. The sorted part now has one item and is never touched again.
  4. Step 4: Repeat on the rest. The second pass does the same thing on a shorter stretch. It scans five items with four comparisons, finds 3 at the far end, and swaps it with the 4 at the front of the unsorted part.
  5. Step 5: Five passes, sorted. Each pass fixes one more item, so six items need five passes; the last item has nowhere else to go. Pass 5 finds the 7 already in place and swaps nothing. The scans get shorter every time: 5, 4, 3, 2, 1.
  6. Step 6: Count the work. Comparisons add up to n(n − 1)/2, which grows with the square of n, written O(n²): double the items and the scanning roughly quadruples. Swaps are different: at most one per pass. So selection sort is slow on big inputs, yet handy when writing to memory is expensive.
  7. Step 7: Not stable. A sort is stable if equal items keep their original order. Selection sort is not: its long-distance swap can throw an item past its twin. Here the first 4 jumps behind the second 4. It matters when the items carry other data, such as rows already sorted by name.
VALUES SO FAR
row7 4 8 2 6 3input
n6input
min····pass 1
row····pass 1
row····pass 2
row····result
compares····O(n²)
swaps····O(n)
stable····rule

Spotted a mistake on this page?