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
| Property | Selection sort | Counting sort |
|---|---|---|
| Technique | Repeatedly pick the smallest remaining value | Count each value, turn the counts into positions and place the values, with no comparisons |
| Best case time | O(n²) | O(n + k) |
| Average case time | O(n²) | O(n + k) |
| Worst case time | O(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
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.
| Input | Selection sort | Counting sort |
|---|---|---|
| Mixed | 29 | 47 |
| Already sorted | 21 | 47 |
| Reversed | 27 | 47 |
| Nearly sorted | 23 | 47 |
| Duplicates | 31 | 47 |
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
- 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 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 Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort