Hashing with linear probing
The hash function decides where a key goes. If the slot is taken, linear probing moves on to the next one.
Rules
- h(k) = k mod m
- Collision: next free slot
- Wrap around at the end
Your turn
Three problems on this topic. Separate lists with commas.
- 1 · Hashing
- 2
- 3
Hash table of size 7, h(k) = k mod 7, linear probing. Insert the keys in order. Empty slots as –.
35, 29, 58, 41, 26
Tabelle, Index 0 bis 6 (comma-separated)
Worked example
Hash table of size 7, h(k) = k mod 7, linear probing. Insert the keys in order. Empty slots as –.
28, 40, 74, 71, 9
Worked solution
- 128 mod 7 = 0
- 240 mod 7 = 5
- 374 mod 7 = 4
- 471 mod 7 = 1
- 59 mod 7 = 2
Typical mistake: On a collision go to the next free slot, wrapping around at the end.
Common questions
What is the load factor?
The number of keys divided by the table size.
