Skip to content
SimpleScope

Sorting

Selection sort vs Counting sort

Selection sort and Counting sort both put an array in order, in different ways. Selection sort: repeatedly pick the smallest remaining value. Counting sort: count each value, turn the counts into positions and place the values, with no comparisons.

At a glance

PropertySelection sortCounting sort
TechniqueRepeatedly pick the smallest remaining valueCount each value, turn the counts into positions and place the values, with no comparisons
Best case timeO(n²)O(n + k)
Average case timeO(n²)O(n + k)
Worst case timeO(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

Selection sort: when to choose it

Choose selection sort when you cannot constrain the values, or when memory is fixed. It compares, so it sorts anything with an order defined on it, and it works in place with at most n − 1 swaps. Counting sort needs whole numbers in a known range and allocates an array proportional to that range plus a copy of the input, so a wide range of values makes it expensive or impossible regardless of how few elements there are.

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 at all, so it is not subject to the O(n log n) comparison lower bound, let alone selection sort's O(n²): it is O(n + k) on every input, always. It is stable, which selection sort is not, so it preserves the order of records that share a key — the property that lets radix sort use it as a building block.

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.

InputSelection sortCounting sort
Mixed2947
Already sorted2147
Reversed2747
Nearly sorted2347
Duplicates3147

What the numbers show

Counting sort costs 47 operations on all five arrays, every one of them a write and not a single comparison. Selection sort wins all five at this size — 29 against 47 on the mixed array, 21 against 47 on the sorted one, 27 against 47 on the reversed one, 23 against 47 on the nearly sorted one and 31 against 47 on the duplicates — because 47 of those operations are structural.

Structural in a precise sense: the run zeroes a count array of k + 1 = 10 cells, adds one per value, runs 9 prefix sums over it, then for each of the seven values decrements a counter, writes to the output array and copies back. With k = 9 and n = 7, the count array is bigger than the input. Selection sort by contrast does a fixed 21 comparisons and between 0 and 5 swaps.

The ranking inverts as soon as n grows past k, and then it never comes back. Counting sort's cost is linear in n + k and selection sort's is quadratic in n: for ten thousand values in the same 0–9 range, counting sort does on the order of tens of thousands of operations and selection sort about fifty million. The decision rule is not the array length on its own — it is whether k is small relative to n, and whether the values are whole numbers at all.

See them run

More comparisons