What this shows
Six real sorting algorithms — Bubble, Selection, Insertion, Merge, Quick, and Heap Sort — running step by step on a bar chart, with the comparisons and writes actually counted as they happen. Play through it, step manually, or drag the scrubber to any point in the sort. The comparison and swap counts at the bottom aren't estimates — they're counted from the exact same steps driving the animation, so they're a real measurement of that specific run, not a formula.
The starting arrangement changes the answer
Algorithm Complexity Explained covers best, worst, and average case as a property of which input an algorithm gets, not just its size — the four starting arrangements here (random, sorted, reversed, few unique values) are picked specifically to make that concrete instead of abstract:
- Quick Sort on a sorted array is its worst case, not its best. This implementation picks the last element as the pivot — on data that's already sorted, every partition splits as unevenly as possible. Run it on 30 already-sorted elements and watch the comparison count land at exactly 435 —
n(n-1)/2, the same count Selection Sort always produces, and the textbook definition of Quick Sort's O(n²) worst case actually showing up. - Selection Sort doesn't care what you feed it. Switch between all four presets and its comparison count never moves — it's exactly
n(n-1)/2every time, because it always scans the entire unsorted remainder to find the next minimum regardless of how close to sorted the array already is. Best, worst, and average case are identical for this one algorithm. - Bubble and Insertion Sort both drop to their best case on sorted input — a single pass, comparisons only, no swaps or shifts at all. That's Θ(n), not Θ(n²), and it's directly visible in how few bars ever light up before the run ends.
What the colors mean
- Amber — the two bars currently being compared.
- Red — the two bars currently being swapped.
- Violet — a bar being overwritten without a swap — Insertion Sort's shift, or Merge Sort writing a value back from its temporary array.
- Green — confirmed in its final sorted position. For Bubble, Selection, and Heap Sort this grows incrementally as the algorithm runs; Merge and Quick Sort work on subranges independently, so nothing turns green until the very last step.
FAQ
Why does Merge Sort's write count look high?
Every element gets copied into a temporary array and then written back into the main array during each merge — that's n writes per merge level, across log n levels, which is exactly where Merge Sort's O(n) auxiliary space and its otherwise excellent O(n log n) guarantee come from. It's the tradeoff: Merge Sort is the only one of these six that can't sort in place.
Is Quick Sort really worse than Merge Sort here?
Only on the sorted preset, and only because of the specific pivot choice this implementation uses (always the last element). A random or median-of-three pivot choice avoids this particular worst case — the takeaway isn't "Quick Sort is bad," it's that an algorithm's average-case reputation can hide a worst case that's one specific, realistic input away.
Where do the Big O values next to each algorithm come from?
See Big O Calculator and Complexity Visualizer for what those growth rates actually mean in operation counts, and Algorithm Complexity Explained for the formal definitions.
Is anything I do here sent anywhere?
No — every sort runs entirely in your browser. Nothing here is ever sent to a server.