DSA Notes
Master counting sort — how it breaks the O(n log n) comparison lower bound, the complete O(n+k) algorithm with stability proof, when k must be small for counting sort to be practical, the cumulative count technique for stable output, and applications in radix sort and frequency counting.
Breaking the O(n log n) Barrier
The proven lower bound for comparison-based sorting is Ω(n log n). But this bound only applies to algorithms that determine order exclusively through element comparisons.
Counting sort breaks this barrier by using element *values* directly as array indices. It does not compare elements at all — it counts them. This makes it O(n + k) where k is the range of input values.
Trade-off: It works only on integer (or integer-mappable) inputs with a bounded range. For general data, use comparison sorts.
Implementation
Why right-to-left for stability? Processing from right to left means equal elements that appeared later in the input get placed first (at higher indices within their group), and those that appeared earlier get placed later — maintaining the original relative order.
Handling Negative Numbers and Offsets
Sorting Strings / Objects with Integer Keys
Complexity Analysis
Time — O(n + k)
- Count step: O(n) — one pass through input
- Cumulative sum: O(k) — one pass through count array
- Output step: O(n) — one pass through input
- Total: O(n + k)
When k = O(n), this is O(n) — linear time! This beats the Ω(n log n) comparison lower bound because it does not use comparisons.
Space — O(n + k)
- Output array: O(n)
- Count array: O(k)
- Total: O(n + k)
When to Use Counting Sort
| k = O(n) | range is proportional to input size |
| Example: sorting 1 million ages (range 0-120) | k=121, n=1M → O(n) |
| Example: sorting exam scores (0-100) | k=101 always → O(n) |
| Sorting characters: k=26 (letters) or 256 (ASCII) | always O(n) |
| k >> n | range is much larger than input |
| Example: sorting 10 integers in range [0, 10^9] | k=10^9 >> n=10 |
| Non-integer data | floating point, strings, complex objects |
The Frequency Count Pattern
Even without full counting sort, the counting step alone is extremely useful:
Summary
Counting sort: count occurrences, use cumulative positions to place elements.
| Property | Value |
|---|---|
| Time | O(n + k) |
| Space | O(n + k) |
| Stable | ✅ Yes (with right-to-left pass) |
| In-place | ❌ No |
| Comparison sort | ❌ No — uses integer values directly |
| Constraint | Input must be non-negative integers (or mappable) |
| Practical k | k ≤ 10 × n |
Key insight: Counting sort beats the comparison lower bound by not comparing elements — it uses their values as indices instead.
*Next Lesson: Radix Sort*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Counting Sort — O(n+k) Sorting That Breaks the Comparison Lower Bound.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, sorting, counting, sort
Related Data Structures & Algorithms Topics