1# separate chaining2def put(t, k):3 i = hash(k) % len(t)4 t[i].append(k)01152282334551# linear probing2def put(t, k):3 i = hash(k) % len(t)4 while t[i] is not None:5 i = (i + 1) % len(t)6 t[i] = k60123456
Back table: separate chaining, a pile (list) of keys per slotFront table: linear probing, one key per slotA hopping key: one probe per slot it checksThe same five keys go into two 7-slot tables with slot = hash(k) % 7. Chaining keeps a list in each slot, so a collision only makes a list longer. Linear probing keeps one key per slot and steps right to the next free one, so full slots join into a cluster that every later collision has to walk.

Two ways to handle a collision

  1. Both tables have 7 slots and pick a slot the same way: i = hash(k) % 7. For a small whole number, hash(k) is k itself. So key 15 goes to slot 1, because 15 % 7 = 1. No clash yet.

Clusters

Expected cost

What happens

;; step 1/9 · two-tables
m = 7 slots, h(k) = hash(k) % 7
hash(15) = 15, 15 % 7 = 1

chaining: slot 1 list = [15]
probing:  slot 1 = 15   (1 probe)

The averages 1/2(1 + 1/(1 − α)) for a hit and 1/2(1 + 1/(1 − α)²) for a miss are Sedgewick and Wayne's Proposition M for linear probing in a large table, assuming the hash spreads keys evenly and independently (uniform hashing). Chaining's miss cost of about α compares, Θ(1 + α) with the hashing, is CLRS chapter 11. A 7-slot table is far too small for these limits, so the figure counts its own exact numbers instead. The code is real Python 3: chaining uses t = [[] for _ in range(7)], probing uses t = [None] * 7. The probing put assumes at least one slot is empty, or the while loop would never end.