Sorting
Heap sort vs Counting sort
Heap sort and Counting sort both put an array in order, in different ways. Heap sort: build a max-heap in the array, then repeatedly move the largest value to the end. Counting sort: count each value, turn the counts into positions and place the values, with no comparisons.
At a glance
| Property | Heap sort | Counting sort |
|---|---|---|
| Technique | Build a max-heap in the array, then repeatedly move the largest value to the end | Count each value, turn the counts into positions and place the values, with no comparisons |
| Best case time | O(n log n) | O(n + k) |
| Average case time | O(n log n) | O(n + k) |
| Worst case time | O(n log n) | O(n + k) |
| Extra space | O(1) | O(n + k) |
| Compares values with each other | Yes | No |
| Stable (equal values keep their order) | No | Yes |
| In place (no extra array) | Yes | No |
When to choose each
Heap sort: when to choose it
Choose heap sort when you cannot constrain the values or the memory. It compares, so any ordered type works, and it needs O(1) extra space — the heap is built inside the array. Counting sort needs whole numbers in a known range and allocates O(n + k), which is unusable when the range is wide. Heap sort's O(n log n) worst-case guarantee holds for every input and every key type, which is the broadest guarantee on this page.
Counting sort: when to choose it
Choose counting sort when the keys are small whole numbers and there are many of them. It makes no comparisons, so O(n + k) beats heap sort's O(n log n) once n is large and k stays small, and it is stable where heap sort is not — equal records keep their original order, which matters when you are sorting by one field after another. The requirements are integral keys, a known narrow range, and memory for that range.
The same inputs, counted
These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes). Counting sort makes no comparisons, so its count is writes only. It includes the k + 1 cells of the count array.
| Input | Heap sort | Counting sort |
|---|---|---|
| Mixed | 43 | 47 |
| Already sorted | 53 | 47 |
| Reversed | 40 | 47 |
| Nearly sorted | 50 | 47 |
| Duplicates | 36 | 47 |
What the numbers show
This is the closest pairing in the sorting group. Counting sort is a flat 47 operations on all five arrays; heap sort ranges from 36 to 53, so the two straddle each other. Heap sort wins the duplicates array, 36 against 47, the reversed array, 40 against 47, and the mixed array, 43 against 47. Counting sort wins the already-sorted array, 47 against 53, and the nearly sorted one, 47 against 50.
The pattern is not arbitrary. Heap sort is most expensive on input that is already ascending, because building a max-heap from it moves nearly everything and then every extraction sifts a value back down from the root. Counting sort cannot be affected by input order at all: it counts, prefix-sums and places, and the arrangement of the input never enters the cost.
With k = 9 and n = 7 the two are artificially close — counting sort's count array is bigger than the data, and heap sort's log₂ 7 is under 3. Raise n with k fixed and counting sort pulls away, since n + k grows linearly while 2n log₂ n does not: at ten thousand values in the 0–9 range it is tens of thousands of operations against hundreds of thousands. Widen the range instead and counting sort becomes the wrong tool while heap sort does not change at all.
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 Heap sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort