The integers 5, 26, 41, 18, 16, 4, 13 are to be stored in a closed bucket hash table of length 7. Write a suitable hash function and show where each of the integers would be stored in the table, assuming the order of insertion is from left to right.


Q.) The integers 5, 26, 41, 18, 16, 4, 13 are to be stored in a closed bucket hash table of length 7. Write a suitable hash function and show where each of the integers would be stored in the table, assuming the order of insertion is from left to right.

Subject: Data Structures

Hash Function:

h(key) = key % 7

Insertion Order:

5, 26, 41, 18, 16, 4, 13

Hash Table:

Index | Value
------ | ----
0      | 13
1      | 16
2      | 4
3      | 26
4      | 41
5      | 5
6      | 18

Explanation:

  • 5: h(5) = 5 % 7 = 5, so 5 is stored at index 5.
  • 26: h(26) = 26 % 7 = 3, so 26 is stored at index 3.
  • 41: h(41) = 41 % 7 = 4, so 41 is stored at index 4.
  • 18: h(18) = 18 % 7 = 4, so 18 is stored at index 4. (Collision with 41)
  • 16: h(16) = 16 % 7 = 2, so 16 is stored at index 2.
  • 4: h(4) = 4 % 7 = 4, so 4 is stored at index 4. (Collision with 41 and 18)
  • 13: h(13) = 13 % 7 = 6, so 13 is stored at index 6.

Note: Collisions occur when two or more keys hash to the same index. In this case, collisions are resolved using chaining. We can use a linked list or another data structure to store multiple values at the same index.