When to Use Counting Sort
Fast recall
Quick revision
Core answer
k is the size of the value range. It can beat comparison sorts when k is reasonably close to n, but its O(k) counting storage makes it a poor choice for a huge or sparse range.Whether you know why counting sort can beat the comparison-sort lower bound and can recognise the range-size condition that makes it practical.
Looking only at the number of items and forgetting that memory and running time also depend on the distance between the smallest and largest key.
Explain the frequency array, the O(n + k) cost, the sparse-range boundary, and how cumulative counts make the algorithm stable.
Interview-ready guide
Interview answer
Counting sort is a non-comparison sorting algorithm for integer keys in a small, known range. Instead of comparing two values, it uses each value as an index and counts how many times that value appears.
For example, 100,000 exam scores can only be from 0 to 100. Counting sort needs 101 counters: one pass records the frequencies, and a scan from 0 to 100 produces the sorted scores. The time is O(n + k), where n is the number of items and k is the size of the key range.
A frequency-only version is enough for plain integers. To sort records and keep equal-key records in their original order, cumulative counts give final positions and records are placed into an output array from right to left. Negative values also work by subtracting the minimum value before indexing the count array.
The key range is the deciding limit. A few numbers spread between 0 and one billion would require too much memory and too long a range scan. Counting sort is a strong choice for dense, bounded integer keys; a comparison-based library sort is safer for general values or a wide, unknown range.
When to Use Counting Sort
Direct Answer
Use counting sort when the values are integer keys drawn from a small, known range. It counts how often each key appears, then rebuilds the sorted result in O(n + k) time, where k is the size of the value range. It can beat comparison sorts when k is reasonably close to n, but its O(k) counting storage makes it a poor choice for a huge or sparse range.
Deep Dive & Architecture
Take the input [4, 2, 2, 1, 3]. Its values lie between 1 and 4, so a count array with four useful positions is enough.
- Count: the frequencies for 1 through 4 become
[1, 2, 1, 1]. - Order: visit the counters from the smallest value to the largest.
- Emit: write one
1, two2s, one3, and one4, giving[1, 2, 2, 3, 4].
No pair of input values was compared. That is why the comparison-sort lower bound does not apply: counting sort uses extra knowledge about the keys. Its cost is n visits to count plus k visits to scan the range.
For records, cumulative counts answer a stronger question: what is the final ending position of each key? Placing records from right to left then preserves the order of records with the same key, which makes the sort stable.
Code Example
static int[] countingSort(int[] values, int min, int max) {
int[] counts = new int[max - min + 1];
for (int value : values) {
counts[value - min]++; // offset also supports negatives
}
int[] sorted = new int[values.length];
int out = 0;
for (int i = 0; i < counts.length; i++) {
while (counts[i]-- > 0) {
sorted[out++] = i + min;
}
}
return sorted;
}
// [4, 2, 2, 1, 3] -> [1, 2, 2, 3, 4]
This compact version sorts integer values. Sorting records stably needs cumulative counts and a separate output array.
Common Mistakes & Pitfalls
Looking only at the number of items and forgetting that memory and running time also depend on the distance between the smallest and largest key.
Check your understanding
Answer these without looking back at the article.
- Why can counting sort run faster than O(n log n)?
- How do cumulative counts make counting sort stable?
- How would you support negative integer keys?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.