aha
Back to all demos
aha · Sorting Algorithms

Sorting Algorithms

Three ways to put numbers in order. Watch them race.

You have 50 books on a shelf in random order. You want them sorted by height. There are different ways to do this — some are slow but obvious, some are fast but clever. Press play and watch three different methods compete on the same shelf.

Array size40
Speed40
Bubble Sort
O(n²)
Comparisons: 0Swaps: 0
Quicksort
O(n log n)
Comparisons: 0Swaps: 0
Merge Sort
O(n log n)
Comparisons: 0Swaps: 0
Frequently asked

Why does quicksort look chaotic at first then suddenly settle?

Quicksort recursively partitions around a pivot. Early in the run, every element is being compared to a faraway pivot — so motion looks random. Once partitions get small enough, the array converges to sorted very quickly.

Is bubble sort ever useful?

Almost never in production — its O(n²) cost is brutal at scale. But it's the perfect mental model for sorting because every step is locally obvious: 'compare two neighbors, swap if wrong.'

Why does merge sort use more memory?

It builds intermediate sorted arrays during the merge step. That O(n) extra space is the cost of its guaranteed O(n log n) worst case — quicksort runs in place but has a worst case of O(n²).

Can I share my exact array configuration?

Yes — the URL contains a seed and array size. Anyone who opens your link sees the same starting array.