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
| Property | Quick sort | Heap sort |
|---|---|---|
| Technique | Divide and conquer: partition around a pivot | Build a max-heap in the array, then repeatedly move the largest value to the end |
| Best case time | O(n log n) | O(n log n) |
| Average case time | O(n log n) | O(n log n) |
| Worst case time | O(n²) | O(n log n) |
| Extra space | O(log n) | O(1) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | No | No |
| In place (no extra array) | Yes | Yes |
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).
| Input | Quick sort | Heap sort |
|---|---|---|
| Mixed | 31 | 43 |
| Already sorted | 75 | 53 |
| Reversed | 51 | 40 |
| Nearly sorted | 65 | 50 |
| Duplicates | 29 | 36 |
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
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick sort
- Bubble sort vs Heap sort
- Bubble sort vs Counting sort
- Bubble sort vs Radix sort
- Selection sort vs Insertion sort
- Selection sort vs Merge sort
- Selection sort vs Quick sort
- Selection sort vs Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge sort
- Insertion sort vs Quick sort
- Insertion sort vs Heap sort
- Insertion sort vs Counting sort
- Insertion sort vs Radix sort
- Merge sort vs Quick sort
- Merge sort vs Heap sort
- Merge sort vs Counting sort
- Merge sort vs Radix sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort