Six Sorting Algorithms, Counted: What 50 Elements Actually Cost
Everyone learns that merge sort beats bubble sort. This is the same lesson with the receipts: the exact comparison and write counts each of six classic sorts runs, measured on the same 50 numbers by the code behind the Sorting Algorithm Visualizer.
· 6 min read · every number counted from the shipped visualizer code
Two Families of Sort
The quadratic sorts — bubble, insertion, selection — walk the array comparing neighbours or scanning for a minimum. Each compares on the order of n²/2 pairs, which for 50 elements is exactly 1,225. They differ in what they do between comparisons. Bubble sort swaps every out-of-order neighbour (1,225 writes on average); insertion sort shifts values into place (also ~1,225 writes) but stops early on nearly-sorted input; selection sort finds the minimum and makes just one swap per position — only 91 writes for the whole array, 13× fewer than bubble.
The divide-and-conquer sorts — merge, quicksort, heapsort — break the problem into pieces and never re-compare what they don't need to. On the same 50 elements merge sort and quicksort each did 220–260 comparisons rather than 1,200-plus. That is the O(n log n) versus O(n²) gap made concrete, and it only widens: at 1,000 elements the quadratic sorts do about half a million comparisons while these do around ten thousand.
What We Actually Measured
Each sort in the visualizer is a step-generator that emits one operation at a time — a comparison, a swap, or a direct write. Running those generators headlessly and counting the operations gives the totals below: the mean over 200 seeded random shuffles of the numbers 1–50.
Total operations on 50 random elements
Blue bars are the O(n log n) sorts, grey are the O(n²) sorts. Total ops = comparisons + writes (a swap counts as two writes). Quicksort and merge sort do roughly a fifth of bubble sort's work here.
Comparisons change with input order
| Algorithm | Random | Sorted | Reversed |
|---|---|---|---|
| Quicksort | 260 | 1,225 | 1,225 |
| Merge sort | 222 | 133 | 153 |
| Heapsort | 415 | 434 | 379 |
| Bubble sort | 1,195 | 49 | 1,225 |
| Insertion sort | 658 | 49 | 1,225 |
| Selection sort | 1,225 | 1,225 | 1,225 |
Comparison counts for n = 50. Three things stand out: bubble and insertion sort collapse to 49 comparisons on already-sorted input (their best case, Ω(n)), quicksort does the opposite — its 1,225 on sorted and reversed input is its O(n²) worst case from a bad pivot — and selection sort ignores the input entirely, always making the same 1,225 comparisons.
The quicksort row is the honest caveat. On random data it is the leanest sort here, but the version in the visualizer — and in most textbooks — picks the last element as the pivot, and on ordered input that pivot is always the extreme value, so every partition removes just one element. Real quicksort implementations dodge this with median-of-three or randomized pivots; production sorts go further and switch algorithms entirely (introsort falls back to heapsort, Timsort exploits existing runs).
Merge sort is the steady one: 133 comparisons sorted, 222 random, 153 reversed — barely moving, because it always splits down the middle regardless of what the data looks like. That predictability, plus stability, is why it underpins external sorts and the merge step in Timsort.
Every number here comes from the visualizer's own generators (globalThis.LKSort.ALGOS) run headlessly and counted — the same operations its on-screen counters show. Open the tool, set the size to 50, and the comparison and write totals it reports are these.
Sorting is one of four interactive algorithm visualizers on the site — the others animate pathfinding, data structures, and game-tree search.
Related reading with the same measure-first approach: the game AIs behind this site's playable games run on three search algorithms, benchmarked in Six Games, Three Classic Algorithms.
Watch the Counters Climb
Pick an algorithm, shuffle the bars, and watch the comparison and write counts tick up in real time — then try feeding quicksort a sorted array.
Common questions
Which sorting algorithm is fastest?
On random input it depends on what you count, but for 50 shuffled numbers the LK Forge visualizer's quicksort did the least total work — about 475 operations (260 comparisons, 214 writes) — with merge sort just behind at 508. Bubble sort did roughly five times as much: about 2,419 operations. In real libraries the answer is usually a hybrid (introsort or Timsort) that switches strategy by input, but among the six textbook sorts here, quicksort and merge sort win on average.
Is quicksort always the fastest sort?
No — and the visualizer shows exactly why. Quicksort averages about 260 comparisons on 50 random elements, but on an already-sorted or reversed array it jumps to 1,225 — the full O(n²) ceiling — because a naive last-element pivot is always the smallest or largest value. That is the classic pivot trap. Merge sort, by contrast, stays near 130–220 comparisons regardless of input order.
Why would anyone use selection sort if it is O(n²)?
Because it minimizes writes. On 50 random elements selection sort made only 91 writes versus 1,225 for bubble and insertion sort — about 13 times fewer. When a write is far more expensive than a comparison (flash memory with limited write cycles, or swapping large records), selection sort's "one swap per position" behavior can matter more than its comparison count.
What is the difference between O(n²) and O(n log n) sorts?
The O(n²) sorts — bubble, insertion, selection — compare roughly n²/2 pairs, which for n=50 is about 1,225. The O(n log n) sorts — merge, quicksort, heapsort — divide the problem and compare far fewer: merge and quicksort did 150–260 comparisons on the same 50 elements. The gap widens fast as the array grows; at n=1,000 the O(n²) sorts do about 500,000 comparisons while the O(n log n) sorts do around 10,000.
Which sorts are stable?
Bubble, insertion and merge sort are stable — equal elements keep their original relative order. Selection sort, quicksort and heapsort are not stable in their standard forms. Stability matters when you sort by one key after already sorting by another.
Were these numbers measured or estimated?
Measured. Each figure comes from running the sorting visualizer's own step-generators (exposed as globalThis.LKSort.ALGOS) headlessly and counting the comparison, swap and write operations they emit — the same operations the on-screen counters tally. The random-input figures are the mean of 200 seeded shuffles, so they are reproducible.