Loading…
Loading…
Charting your lesson…
Opening your lesson…
Non-Comparison Sorts — Counting, Radix, Bucket
Lesson 4 of 7
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Comparison sorts can't beat O(n log n), but when keys are integers or strings in a bounded range, counting, radix and bucket sort break that barrier and reach O(n). Knowing when this is legal — and when it isn't — is a sharp interview differentiator.
Core idea: instead of comparing elements, use the keys themselves as array indices (or digit buckets). This sidesteps the comparison lower bound entirely, achieving linear time when the key range is small relative to n.
Step 2/ 61 min
For integers in [0, k]: count each value's occurrences, then rebuild the array from the counts.
count[v] += 1 for each v # tally
# prefix-sum counts, then place each element at its position
O(n + k) time, O(k) space. Linear when k = O(n); it is also stable if you place elements right-to-left using prefix sums.
Sort integers (or fixed-length strings) digit by digit, least-significant first, using a stable counting sort per digit.
d digits, each pass O(n + base) → O(d·(n + base)), effectively O(n) when d and base are small/constant.Bucket sort distributes n elements (assumed roughly uniformly distributed) into n buckets, sorts each bucket (insertion sort), and concatenates → O(n) average, O(n²) worst if all land in one bucket. The universal caveat: these sorts are linear only when the key range/digit count is bounded. For arbitrary 64-bit keys or general comparables, radix's d factor or counting's k blows up and comparison sorts win. They also need extra space and only apply to integer-like keys, not arbitrary objects.
Step 3/ 61 min
Counting sort [4, 2, 2, 8, 3, 3, 1], k=8. Counts: 1:1, 2:2, 3:2, 4:1, 8:1. Emit in value order: [1, 2, 2, 3, 3, 4, 8]. Total work O(n + k) = O(7 + 9) — linear, no comparisons made. If k were, say, 10⁹ this would waste huge memory and lose to O(n log n).
Step 4/ 61 min
Step 5/ 61 min
Applying counting sort to values with a huge range (e.g. arbitrary 32-bit integers) and expecting O(n). The k term dominates — you'd allocate a gigantic count array. Counting sort is O(n) only when k = O(n); otherwise use radix (bounded digits) or a comparison sort.
Step 6/ 6· Worked examples1 min
Problem
Sort [4, 2, 2, 8, 3, 3, 1] using counting sort (values are small integers).
Try it on paper first.
Recall
Keep this
Usage: use counting/radix only when the key range is O(n) or the width is small.
Comparison sorts are bounded below by O(n log n), but when keys are integers or strings in a bounded range you can beat that with counting, radix, and bucket sort — achieving linear time by not comparing elements. Knowing when this is legal (and its space cost) is the interview point.
| Sort | Time | Space | Stable | Requires |
|---|---|---|---|---|
| Counting | O(n + k) | O(n + k) | Yes | small integer range |
| Radix (LSD) | O(d(n + k)) | O(n + k) | Yes | fixed-width keys |
| Bucket | O(n) avg / O(n²) | O(n) | depends | uniform distribution |
"Sort integers in [0, k]", "sort by digits/characters", maximum gap (bucket sort), or "can you beat O(n log n)?". Interviewers check the range/structure precondition and the space trade-off.
Counting (O(n+k)), radix (O(d(n+k))), and bucket (O(n) avg) sorts beat the O(n log n) comparison bound by exploiting bounded integer/string keys instead of comparing. Counting/radix are stable; watch counting sort's O(k) space when the range is large.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.