Bubble sort
Walks the array over and over, swapping neighbours that are out of order, until a pass changes nothing. The slowest of the useful sorts and the easiest to see — which is why everyone meets it first.
Insertion sort
The way you sort a hand of cards: lift the next value out, slide everything bigger to the right, drop it in the gap. Its cost tracks how far from sorted the input already was, which is why real libraries fall back to it for short runs.
Selection sort
Scan the remainder, find the smallest, swap it into place, repeat. It never gets lucky — the comparison count is identical for every input of a given size — but it never does more than n−1 swaps.
Merge sort
Split until the pieces are single values, then merge them back in order. No input degrades it — the guarantee is why it is used where latency has to be predictable. It buys that with O(n) scratch space, shown on the page.
Quick sort
Pick a pivot, shove everything smaller to its left, and it lands where it belongs permanently. Usually the fastest of the lot — but this textbook version degrades to O(n²) on sorted input, which you can trigger from the presets.
Heap sort
Read the array as a binary tree, arrange it so every value sits above its children, then pull the root off the top n times. The only sort here that is both guaranteed O(n log n) and needs no extra memory.
Race them against each other
Every sort gets the same array and advances one comparison or write per tick, all at once. The first to finish did the least work — and which one that is depends on the input far more than you might expect.
Try the same input on all six
- Already sorted
- Bubble and insertion finish in n−1 comparisons. Selection still grinds through the full n(n−1)/2. Quick sort collapses — this is its worst case, not its best.
- Reversed
- The worst case for bubble and insertion, and merely average for merge and heap. Quick sort suffers here too, for the same reason as above.
- Nearly sorted
- Insertion sort barely writes at all — its cost tracks the number of inversions, and there are almost none. Nothing else notices.
- Few unique
- Lots of equal values. Watch which sorts keep them in their original order and which throw them around: that is what stable means.
- Anything, at n = 60
- The quadratic three do around 1,770 comparisons. Merge and heap do a few hundred. This is the whole reason O(n log n) matters.
The whole run is computed before the first frame is drawn. Each algorithm executes once against your array and records what it did — every comparison, every swap, and the line responsible. Playback is then just an index into that recording, which is why you can scrub backwards through a sort and change the speed halfway. Nothing you type leaves your machine.