Question 16a
Given the values 45,36,19,58 and 10 and a hash table of size 13. Show the resultant table after inserting values in the given order by using hash function h(k)=k%13 with each of the following collision strategies. i) Linear Probing ii) Quadratic Probing iii) Double hashing with secondary hash function h'(k)=7-k%7
Topics: Collision resolution: linear probing, quadratic probing, double hashing
Watch this exact question run
Other parts: Quadratic probingDouble hashing
Solution
- Quadratic: (h + i²) mod 13. Double hashing: (h + i·h′(k)) mod 13 with h′(k) = 7 − (k mod 7).
| Key | 45 | 36 | 19 | 58 | 10 |
|---|---|---|---|---|---|
| k mod 13 | 6 | 10 | 6 | 6 | 10 |
| h′(k) = 7 − k mod 7 | 4 | 6 | 2 | 5 | 4 |
| Key | Home h(k) | Probes | Stored at |
|---|---|---|---|
| 45 | 6 | (6 + 0) mod 13 = 6 | 6 |
| 36 | 10 | (10 + 0) mod 13 = 10 | 10 |
| 19 | 6 | (6 + 0) mod 13 = 6 → (6 + 1) mod 13 = 7 | 7 |
| 58 | 6 | (6 + 0) mod 13 = 6 → (6 + 1) mod 13 = 7 → (6 + 2) mod 13 = 8 | 8 |
| 10 | 10 | (10 + 0) mod 13 = 10 → (10 + 1) mod 13 = 11 | 11 |
Final table — 0: — | 1: — | 2: — | 3: — | 4: — | 5: — | 6: 45 | 7: 19 | 8: 58 | 9: — | 10: 36 | 11: 10 | 12: —
| Key | Home h(k) | Probes | Stored at |
|---|---|---|---|
| 45 | 6 | (6 + 0²) mod 13 = 6 | 6 |
| 36 | 10 | (10 + 0²) mod 13 = 10 | 10 |
| 19 | 6 | (6 + 0²) mod 13 = 6 → (6 + 1²) mod 13 = 7 | 7 |
| 58 | 6 | (6 + 0²) mod 13 = 6 → (6 + 1²) mod 13 = 7 → (6 + 2²) mod 13 = 10 → (6 + 3²) mod 13 = 2 | 2 |
| 10 | 10 | (10 + 0²) mod 13 = 10 → (10 + 1²) mod 13 = 11 | 11 |
Final table — 0: — | 1: — | 2: 58 | 3: — | 4: — | 5: — | 6: 45 | 7: 19 | 8: — | 9: — | 10: 36 | 11: 10 | 12: —
| Key | Home h(k) | Probes | Stored at |
|---|---|---|---|
| 45 | 6 | (6 + 0×4) mod 13 = 6 | 6 |
| 36 | 10 | (10 + 0×6) mod 13 = 10 | 10 |
| 19 | 6 | (6 + 0×2) mod 13 = 6 → (6 + 1×2) mod 13 = 8 | 8 |
| 58 | 6 | (6 + 0×5) mod 13 = 6 → (6 + 1×5) mod 13 = 11 | 11 |
| 10 | 10 | (10 + 0×4) mod 13 = 10 → (10 + 1×4) mod 13 = 1 | 1 |
Final table — 0: — | 1: 10 | 2: — | 3: — | 4: — | 5: — | 6: 45 | 7: — | 8: 19 | 9: — | 10: 36 | 11: 58 | 12: —
How to write it in the exam
| Index | Linear | Quadratic | Double hashing |
|---|---|---|---|
| 0 | — | — | — |
| 1 | — | — | 10 |
| 2 | — | 58 | — |
| 3–5 | — | — | — |
| 6 | 45 | 45 | 45 |
| 7 | 19 | 19 | — |
| 8 | 58 | — | 19 |
| 9 | — | — | — |
| 10 | 36 | 36 | 36 |
| 11 | 10 | 10 | 58 |
| 12 | — | — | — |
Double hashing details: 19 → 6 occupied → (6 + 1·2) = 8. 58 → 6 occupied → (6 + 1·5) = 11. 10 → 10 occupied → (10 + 1·4) mod 13 = 1.
How this answer was verified
- Hand computation
- tests/hashing-search.test.ts
- hashing_open_addressing.c differential test