The Lab
Original — built for the Lab

What Sorting Sounds Like

On the same 1,024 shuffled values, insertion sort makes about 265,000 comparisons and merge sort about 9,600. Here you watch every one of them light up, hear each as a note, and race four algorithms side by side on identical input until the difference between n² and n log n stops being a formula.

AlgorithmsSortingWeb AudioCanvasComplexity
After the sorting films of Ronald Baecker, 'Sorting Out Sorting', 1981, and the audibilisations of sorting that followed
Loading demo…

Every sorting algorithm is a strategy for a very small question asked a great many times: is this one bigger than that one? Bubble sort asks it about neighbours and swaps them when they are wrong. Insertion sort picks up each value and slides it back to where it belongs. Merge sort sorts two halves and zips them together. Quicksort throws everything to one side or other of a pivot and repeats. They all end in the same place. What differs is how many questions they ask on the way.

The surprise is how quickly that difference grows. Double the row and the slow sorts do four times the work, because they compare most values with most others. The fast ones do only a little over twice as much, because they keep halving the problem. At sixteen values nobody can tell them apart. At 1,024, insertion sort asks around 265,000 questions and merge sort fewer than 10,000. The table under the stage is measured on the exact values you are looking at, and the tile beside it divides the comparisons by n log₂ n: the fast sorts sit near one at every size, while the slow ones climb as the row grows.

The sound turns this into something you can hear before you can count it. Each value has its own pitch, so a comparison is a note and a sort is a melody. Quicksort chatters in bursts that narrow as the pieces get smaller. Merge sort rises in runs. Heap sort sounds like a ball dropping down a staircase over and over. Radix sort never compares two values at all — it deals them into ten piles by their last digit, then the next — and it sounds unlike anything else. When a sort finishes, a gold sweep runs up the row and plays the scale it built.

None of this is academic. Every spreadsheet sort, database index and search result page runs on one of these ideas, and the gap you can hear at 1,024 values becomes hours at a few million. Bogosort is here too, shuffling at random and checking whether it got lucky. It is a joke, but an honest one: the tile tells you how long it should expect to take, and at sixteen values the answer is longer than people have existed.

Next in the Lab
InkOpen

Pour glowing ink into black water and drag it into vortices, billows and mushroom plumes — a real fluid, solved sixty times a second.