Counting Sort and LSD Radix Sort
Compare counting by key with stable passes over digits starting at the least significant digit.
Core Intuition & How It Works
Counting sort and LSD radix sort bypass the O(n log n) comparison lower bound by indexing directly into key buckets or sorting digit by digit from least to most significant. When key range or digit count is small, they run in linear time.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(n + k) counting, O(d(n + b)) radix |
| Space Complexity | O(n + k) |
| Topic Chapter | Chapter 9: Sorting & Selection |
| Difficulty Level | Intermediate |
Key Pattern to Recognize
Iterate backward when placing elements via cumulative counts in counting sort so equal keys stay stable—essential for LSD radix sort.
Common Pitfalls & Edge Cases
Using counting sort when key range k is huge compared to n, or failing to handle negative numbers via an offset.
Real-World & Technical Interview Applications
Sorting fixed-width integers, IP addresses, suffix array construction, and GPU parallel radix sorts.