Sub topic 3
The Omega(n log n) Lower Bound for Comparison Sorts
This chapter assumes you know loops, recursion, logarithms, big-O notation, and how to trace a hash table by hand.
Why comparison sorts need Omega(n log n) comparisons
A comparison sort learns about its input only by comparing two elements. Model any such algorithm as a decision tree, where each internal node is one comparison and each leaf is one output ordering. For n distinct items there are n! orderings, so the tree needs at least n! leaves. A binary tree of height h has at most 2^h leaves, so h >= log2(n!). By Stirling's approximation, log2(n!) = Theta(n log n). The worst-case comparison count is therefore Omega(n log n).
Merge sort and heap sort meet this bound, so they are optimal up to a constant factor.
Counting, radix, and bucket sort
Counting sort avoids comparisons by counting keys in the range 0 to k-1, in O(n + k) time. Radix sort runs a stable counting sort on each digit, giving O(d(n + b)) time for d digits in base b. These sorts beat the bound because they use key values, not comparisons. Bucket sort runs in expected linear time when keys are spread uniformly.
Hash tables: keys, slots, and collisions
A hash table stores keys in m slots. A hash function h maps each key to a slot index. A collision occurs when two keys map to the same slot.
Designing hash functions
The division method uses h(k) = k mod m. Choose m to be a prime that is not close to a power of 2. The multiplication method uses h(k) = floor(m frac(k A)) with A about 0.618.
Collision resolution by chaining
Each slot holds a linked list of keys. With load factor alpha = n/m, an unsuccessful search expects 1 + alpha probes, and a successful search expects 1 + alpha/2.
Open addressing: linear, quadratic, and double hashing
Open addressing stores every key in the table itself. Linear probing tries h(k) + i, which causes primary clustering. Quadratic probing tries h(k) + c1i + c2i^2, which reduces primary clustering but still causes secondary clustering. Double hashing tries h1(k) + i*h2(k) with a nonzero h2(k).
def insert_double(table, key):
m = len(table)
h1 = key % m
h2 = 1 + key % (m - 1)
for i in range(m):
slot = (h1 + i * h2) % m
if table[slot] is None:
table[slot] = key
return slot
raise OverflowError("table full")Load factor and expected probe counts
The load factor is alpha = n/m. Under uniform hashing with open addressing, an unsuccessful search expects 1/(1 - alpha) probes, and a successful search expects (1/alpha) * ln(1/(1 - alpha)) probes. Both formulas need alpha < 1. At alpha = 0.5, the successful search expects about 1.39 probes.
GATE problem patterns and strategies
- Count comparisons or solve recurrences with the Master theorem.
- Trace each probe by hand when a question gives a table and a hash function.
- Check stability and in-place behavior before choosing a sort.
Exercise
A table has m = 10 slots and holds n = 5 keys under open addressing. What is the expected number of probes for an unsuccessful search?
Show answer
alpha = 0.5, so 1/(1 - 0.5) = 2 probes.