Sub topic 13
Quadratic Probing and Double Hashing
This chapter assumes you know how linear probing works, including tombstones for deletion, and what load factor means. Linear probing has one weakness: it clusters. Quadratic probing and double hashing change the step pattern to spread entries out.
The problem with linear probing
Linear probing tries slots h, h+1, h+2, and so on. Once a few keys fill neighboring slots, any key that hashes into that run must walk to its end and then extends the run. This is called primary clustering. Long runs make both lookups and inserts slower.
Quadratic probing
Quadratic probing jumps further with each attempt. The i-th probe for a key with hash h in a table of size n is:
slot(i) = (h + i * i) % nThe offsets are 0, 1, 4, 9, 16, and so on. Two keys that start at different slots no longer follow the same path, so primary clustering disappears. Keys with the same starting slot still share one probe sequence. This milder effect is called secondary clustering.
Double hashing
Double hashing uses a second hash function to choose the step size:
slot(i) = (h1 + i * h2) % nKeys that collide at h1 usually have different h2 values, so they follow different paths. This is the closest open addressing gets to ideal random probing.
Seeing the sequences
def linear(h, n, count):
return [(h + i) % n for i in range(count)]
def quadratic(h, n, count):
return [(h + i * i) % n for i in range(count)]
def double_hash(h1, h2, n, count):
return [(h1 + i * h2) % n for i in range(count)]
print(linear(3, 11, 6)) # [3, 4, 5, 6, 7, 8]
print(quadratic(3, 11, 6)) # [3, 4, 7, 1, 8, 6]
print(double_hash(3, 6, 11, 6)) # [3, 9, 4, 10, 5, 0]
# Quadratic probing in a table of size 8 revisits the same slots.
print(sorted(set(quadratic(3, 8, 8)))) # [3, 4, 7]The last line shows the main risk. With n = 8, the eight probes reach only three distinct slots. A table can have empty slots that the probe sequence never visits, so an insert can fail or loop.
Making the sequence cover the table
Each scheme needs a rule that guarantees the probe sequence reaches a free slot.
| Scheme | Safe choice | Guarantee |
|---|---|---|
| Quadratic | Prime `n`, load factor at most 0.5 | Always finds an empty slot |
| Quadratic | Power-of-two `n`, offsets `i*(i+1)/2` | Visits every slot |
| Double hashing | `h2` never 0, and coprime with `n` | Visits every slot |
| Double hashing | Prime `n`, `h2 = 1 + key % (n - 1)` | Visits every slot |
| Double hashing | Power-of-two `n`, `h2` always odd | Visits every slot |
If h2 were 0, every probe would land on the same slot. If h2 shared a factor with n, the sequence would cycle through only part of the table.
Trade-offs
- Quadratic probing keeps nearby probes cheap for the CPU cache early on, but later probes jump far away.
- Double hashing gives the best spread and needs the fewest probes at high load. It costs a second hash computation, and its probes are scattered, which is less cache-friendly.
- Linear probing is often fastest in practice at moderate load because of its cache behavior, despite clustering.
Exercise
A table has n = 8 slots. You use double hashing with h2 = 4 for a key whose h1 = 2. Why is this a bad choice, and which slots will the first four probes visit?
Show answer
The step 4 shares a factor with 8, so the sequence cycles through only two slots. The probes visit (2 + i*4) % 8 for i = 0..3, giving 2, 6, 2, 6. Any empty slot elsewhere is never found. Choosing an odd h2 fixes this.