Sub topic 14
Counting, Radix, and Bucket Sort
Assumptions
You should know arrays, loops, functions, logarithms, and asymptotic notation. You should also understand that O describes an upper growth rate. This chapter focuses on sorting methods that can avoid the comparison-sorting lower bound of Ω(n log n) when their input has extra structure.
Counting Sort
Counting sort is a non-comparison sort for integers in a small range. If every value is between 0 and k - 1, it counts how often each value occurs.
The algorithm has three stages:
- Create a count array of size
k. - Count each input value.
- Write each value back according to its count.
def counting_sort(values, k):
counts = [0] * k
for value in values:
if value < 0 or value >= k:
raise ValueError("value outside the supported range")
counts[value] += 1
result = []
for value, count in enumerate(counts):
result.extend([value] * count)
return result
print(counting_sort([3, 0, 2, 3, 1, 2], 4))If n is the number of elements, counting sort takes O(n + k) time and O(n + k) space in a stable implementation. It is useful when k is not much larger than n. If values range from 0 to one billion but only ten values occur, allocating one billion counters is wasteful.
Radix Sort
Radix sort processes fixed-width keys one digit at a time. For decimal integers, it may sort by units, tens, hundreds, and so on. Each pass must use a stable sort, commonly counting sort. Stability preserves the order established by earlier digit passes.
With d digits and base b, the time complexity is O(d(n + b)). For fixed-width machine integers, d and b can be treated as constants, giving linear time in n.
Bucket Sort
Bucket sort distributes values into intervals called buckets, sorts each bucket, and concatenates the buckets. It performs well when values are approximately uniformly distributed. Its expected time can be O(n + k), but a poor distribution can place nearly every value in one bucket, producing O(n²) time if insertion sort is used inside that bucket.
| Algorithm | Main assumption | Typical time | Stable by default |
|---|---|---|---|
| Counting | Small integer range | O(n + k) | Yes, with output positions |
| Radix | Fixed number of digits | O(d(n + b)) | Yes, if each pass is stable |
| Bucket | Nearly uniform distribution | Expected O(n + k) | Depends on bucket sort |
Hash Tables: Keys, Slots, and Collisions
A hash table stores key-value records in an array of slots. A hash function maps a key to an initial slot:
h(key) = key mod m
where m is the table size. A collision occurs when different keys map to the same slot. A table must resolve collisions through chaining or open addressing. Chaining keeps a collection at each slot. Open addressing searches for another empty slot.
For example, with m = 7, keys 10 and 17 both map to slot 3 because both leave remainder 3. The collision is expected; the resolution method determines where the records are stored.
Common Errors
- Using counting sort when the value range is enormous.
- Forgetting that radix passes must be stable.
- Claiming bucket sort is always linear.
- Treating a hash collision as an algorithm failure.
- Confusing the key with the slot index produced by the hash function.
Exercise
Which method is a better choice for 1,000 integers whose values lie between 0 and 20: comparison sort or counting sort?
Show answer
Counting sort, because its O(n + k) cost is small when k = 21.