Thought Toys · Strategy & computation · Exhibit 23

Sorting algorithms

The same shuffled bars, sorted five different ways. Some methods plod — comparing every pair, again and again. Others split the work cleverly and finish in a blink. Watch the comparison counter, then drag the pile bigger: the slow ones don't just lose, they lose by more and more. That widening gap is what "big-O" really means.

Vertical bars of different heights being rearranged into ascending order.

Choose a method — slow O(n²) vs fast O(n log n)

What you're seeing

Every bar is a number; the job is to line them up shortest-to-tallest. Each method gets the same shuffled row, and we count what it actually does — every comparison (is this bar taller than that one?) and every move. The two bars under comparison glow amber; a bar that just moved flashes and fades.

The three plodders all share a flaw: they compare neighbours over and over. Bubble sweeps the row swapping out-of-order pairs, again and again. Selection scans the whole rest of the row each time to find the next smallest. Insertion slides each new bar back into the sorted part. On a pile of n bars they each do roughly n²⁄2 comparisons — the work grows with the square of the size.

The two quick ones don't compare everything to everything. Merge sort splits the row in half, sorts each half, then merges the two sorted halves in one pass. Quicksort picks a pivot and throws smaller bars left, bigger right, then repeats on each side. Both finish in about n·log₂n steps — and that is a different kind of number. At a few bars the two kinds look the same; the real story is what happens when you drag the size up. Doubling the pile roughly doubles a fast sort's work, but quadruples a slow one's — so the gap doesn't just persist, it widens, forever. At 70 bars an O(n²) sort already does six times the work of an O(n log n) one; at 70 thousand it would do thousands of times more. That is the whole reason "which algorithm" matters more than "which computer." (The sort your computer actually runs is usually Timsort — a hybrid that does plain insertion sort on tiny chunks, where its low overhead wins, then merges them: it exploits exactly the crossover you're looking at.)

The rule, exactly. Count key comparisons. The plodders are quadratic — bubble & selection & insertion (worst): C = n(n−1)⁄2 ≈ n²⁄2 while a good split-and-merge is log-linear — merge & quicksort: Cn · log₂ n so their ratio n²⁄2 ÷ n·log₂n = n⁄(2 log₂n) grows with n — the gap has no ceiling. Verified in node (improve/verify/23-sorting.js): all five sorts return a sorted permutation; the exact laws hold (bubble & selection & insertion hit n(n−1)⁄2 in their worst case, bubble & insertion hit n−1 already-sorted; merge stays ≤ n⌈log₂n⌉, quick comfortably under n²⁄4 on random data — its average, not a guarantee) and the O(n²):O(n log n) ratio strictly increases with n. Counter-example: a bubble sort with its inner bound off by one stops one pair short and leaves a reversed row unsorted — the proof's check catches it, so "sorted" means sorted, not assumed.

Also in Strategy & computation: PageRank & the random surfer →

All 23 in Strategy & computation
  1. 10The evolution of trust
  2. 23Sorting algorithms — you are here
  3. 24PageRank & the random surfer
  4. 25Huffman coding
  5. 26Dijkstra's shortest path
  6. 27Nash equilibria
  7. 33The learning-rate cliff
  8. 36A* pathfinding
  9. 37Braess's paradox
  10. 44Diffie–Hellman key exchange
  11. 45Preferential attachment
  12. 46Aliasing & the Nyquist limit
  13. 47The secretary problem
  14. 49Freeze too fast, stay stuck
  15. 51Cross one line, and its territory closes
  16. 56Catch one error, miss the next
  17. 57Why more processors stop helping
  18. 58Why a busy line explodes
  19. 59The set that's only sure when it says no
  20. 60The fit that memorizes instead of learns
  21. 61When the wire breaks, pick one
  22. 67Better at both, and still better off trading
  23. 69Everyone was consistent. The vote wasn't.

← the cabinet · Thought Toys — a cabinet of explorable explanations. Exhibit 23.