CODE THE HERO

Home › Learn › World 5: Sorting Factory

PREMIUM

Selection Belt

Selection sort, O(n²) compares, ≤ n swaps

Level 5.1 of Code the Hero, in Sorting Factory: selection, insertion, merge and quick sort.

What you practise

Selection sort, O(n²) compares, ≤ n swaps. You write real Python, Java or JavaScript, press Run, and the hero acts out every line, so you can see why the fast way is fast.

What it costs

ComparisonsO(n²)
SwapsO(n)
Extra spaceO(1)

How the stars are earned

  1. Sorted on every test
  2. At most n²/2 swaps
  3. At most n − 1 swaps

Your code is also checked on hidden inputs, so it has to work for any case, not only the one on screen.

▶ Start free with World 1See Premium

Worlds 1–3 (21 levels) are free. Sorting Factory is part of Premium.

More in Sorting Factory