Skip to content
SimpleScope

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

PropertyHeap sortCounting sort
TechniqueBuild a max-heap in the array, then repeatedly move the largest value to the endCount each value, turn the counts into positions and place the values, with no comparisons
Best case timeO(n log n)O(n + k)
Average case timeO(n log n)O(n + k)
Worst case timeO(n log n)O(n + k)
Extra spaceO(1)O(n + k)
Compares values with each otherYesNo
Stable (equal values keep their order)NoYes
In place (no extra array)YesNo

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.

InputHeap sortCounting sort
Mixed4347
Already sorted5347
Reversed4047
Nearly sorted5047
Duplicates3647

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