How linear probing makes keys pay for collisions they never had.
About this comparison
Seven keys, 26, 81, 49, 15, 72, 93 and 62, go into the same table of 11 buckets in the same order. One board stores collisions in a list under each bucket; the other keeps one key per slot. Then both look up 93.
Separate chaining hashes a key to its bucket, the key mod 11, and hangs it at the end of that bucket’s list. A collision costs a look at each key already in that list, and no other bucket is touched.
Linear probing keeps one key per slot in a flat array. If the home slot is taken, it tries the next slot to the right, then the next, until one is free. A search follows the same path.
A probe is one slot examined, on both boards. Chaining pays only for keys that hashed to the same bucket: O(1 + α) on average, where α is the load factor. Probing also pays for keys from other buckets parked nearby: a run of full slots blocks every key that hashes into it. That is primary clustering and it grows fast as the table fills.
The keys hash to 4, 4, 5, 4, 6, 5 and 7. 26 and 81 collide: chaining hangs 81 under 26, probing puts it in slot 5. Then 49 hashes to 5. Chaining finds bucket 5 empty, but probing hits 81, a key that never hashed there. Every later key pays for the run: 93 walks past four taken slots to slot 9. Probing ends with slots 4 to 10 full, one run of 7, while chaining holds the same 7 keys at most 3 deep, in bucket 4.
Then the lookup: chaining finds 93 second in its list, after 2 probes; probing walks 5 slots from its home at 5 to slot 9.
Use chaining when the table may get crowded or entries are deleted often: a long list slows one bucket, and a delete moves nothing else. Java’s HashMap works this way.
Use linear probing when memory layout matters and the load stays low. With no list nodes, a short walk along one array is cheap for the CPU cache. Keep it well under full: here, at a load of 0.64, one cluster already doubled the work.