Skip to content
SimpleScope

Sorting

Quick sort vs Heap sort

Quick sort and Heap sort both put an array in order, in different ways. Quick sort: divide and conquer: partition around a pivot. Heap sort: build a max-heap in the array, then repeatedly move the largest value to the end.

At a glance

PropertyQuick sortHeap sort
TechniqueDivide and conquer: partition around a pivotBuild a max-heap in the array, then repeatedly move the largest value to the end
Best case timeO(n log n)O(n log n)
Average case timeO(n log n)O(n log n)
Worst case timeO(n²)O(n log n)
Extra spaceO(log n)O(1)
Compares values with each otherYesYes
Stable (equal values keep their order)NoNo
In place (no extra array)YesYes

When to choose each

Quick sort: when to choose it

Choose quick sort when the data is in mixed order and you want the best average performance in place. Balanced partitions make it the cheapest comparison sort on this page on random-looking input, with good locality — a partition is two scans towards each other — and only O(log n) of recursion stack. What you accept is a quadratic worst case and instability. With the last element as pivot it is at its worst on sorted input, which is a common shape in practice, so harden the pivot.

Heap sort: when to choose it

Choose heap sort when the worst case has to be bounded and memory is tight. O(n log n) in every case with O(1) extra space, and no input can degrade it — there is no pivot to pick badly. The costs are instability, poor locality from jumping between parents and children, and a larger constant than quick sort's on typical data. If a tail-latency guarantee matters more than the average, this is the trade you want.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).

InputQuick sortHeap sort
Mixed3143
Already sorted7553
Reversed5140
Nearly sorted6550
Duplicates2936

What the numbers show

The two algorithms split the five cases along exactly the line their complexity rows predict. Quick sort wins where partitions are balanced: 31 operations against 43 on the mixed array, with only 11 comparisons, and 29 against 36 on the duplicates. Heap sort wins the three ordered or reversed cases — 50 against 65 on the nearly sorted array, 40 against 51 on the reversed one — and on sorted input the margin is large: 53 against 75.

That 75 on sorted input is quick sort's O(n²) worst case, not a measurement artefact. Lomuto partitioning with the last element as pivot finds every element less than or equal to it, so the pivot ends up at the far end and the recursion removes one element per level: 21 comparisons and 54 writes. Heap sort's own most expensive input is also the sorted array at 53, but its range across all five is only 36 to 53, against quick sort's 29 to 75.

Variance, not average, is the real difference. Heap sort's five totals span a factor of 1.5; quick sort's span 2.6, and the bad end is produced by an input you should expect to see. In production the usual resolution is to keep quick sort's average and remove its tail — randomise or median-of-three the pivot, and fall back to heap sort once the recursion depth exceeds about 2 log₂ n, which is what introsort does.

See them run

More comparisons