// algorithms, visualized

Click to Sort: A Visual Walkthrough of Three Classic Algorithms

O(n²) vs O(n log n) — click Run and watch the difference happen.

Vimal MaheedharanAlgorithms · Interactive
01 / Bubble Sort

Repeatedly compares neighbors and swaps them if they're out of order. Click Worst to feed it a reverse-sorted array and watch the comparison count climb.

Click Run to begin.

Speed
Comparing Swapping Sorted
n = 10 Comparisons: 0 / n²=100 Swaps: 0
02 / Merge Sort

Splits the array in half, sorts each half, merges them back in order. No swaps — writes. Same effort no matter the input, which is exactly the point.

 

Click Run to begin.

Speed
Comparing Swapping Sorted
n = 10 Comparisons: 0 / n·log₂n≈33 Writes: 0
03 / Quick Sort

Picks a pivot, partitions smaller left / larger right, recurses. Click Worst to see already-sorted data quietly drag it back to O(n²). Watch for the violet PIVOT marker — that's the one choice that decides everything else.

Click Run to begin.

Speed
Comparing Swapping Sorted Pivot
n = 10 Comparisons: 0 / n·log₂n≈33 Swaps: 0
Run all three on the same n and the comparison counters tell the whole story: bubble sort's count grows roughly with the square of the array size, while merge and quick sort's grow far more gently. That gap is invisible in a textbook. It's obvious after thirty seconds of clicking Run — and the theoretical n² / n·log₂n figures next to each counter are there so you can check the real count against the formula directly, not just trust it.

The trade-offs, side by side

AlgorithmTime (avg)SpaceStable?Where it earns its place
Bubble SortO(n²)O(1)YesTeaching, tiny or nearly-sorted arrays
Merge SortO(n log n)O(n)YesGuaranteed performance, external/linked-list sorts
Quick SortO(n log n)O(log n)NoGeneral-purpose default, in-memory arrays

None of these are "the best" sort in the abstract — that's the same trap as picking a database index without checking the actual workload. Bubble sort is genuinely fine for sixteen elements. Merge sort's O(n) space cost is a real trade for its guaranteed O(n log n) ceiling. Quick sort's average-case speed comes with a worst case you should actually know about, not just accept on faith. Run the three panels above again with that in mind.

← Return to Home LinkedIn ↗