A Sorting II
12 — Algorithms
Divide & conquer — break the n— barrier

Merge & Quicksort

Split the problem in half, sort the halves, combine. That single idea drops sorting from O(n²) to O(n log n) — the speed real systems rely on.

The simple sorts were quadratic because each element was compared against many others. Divide and conquer escapes that: split the array in half, sort each half, then stitch the results together. With log n levels of splitting and O(n) work per level, the total is O(n log n). Merge sort splits all the way down, then merges sorted halves back up — rock-solid O(n log n) always, and stable, but needs O(n) extra space. Quicksort picks a pivot, partitions values around it, and recurses — usually the fastest in practice and in-place, though a bad pivot can degrade it to O(n²).

Watch divide & conquer in 3D

Bar height is value. Merge sort highlights the sub-range it is merging; quicksort highlights its pivot and the partition boundary. Counters show the O(n log n) advantage.

Drag to rotate — scroll to zoom
ready
speed
Press a sort to watch divide & conquer run.ready
Active sub-range Comparing / pivot Placing / swapping Sorted / locked

The two champions

Both are O(n log n) on average — but they make very different trade-offs around space, stability, and worst case.

Cost cheat sheet

The leap from the quadratic sorts to these is the difference between thousands and millions of operations on real data.