Hash function h(x) = ((ord(x) – ord(A) + 1)) %10
Given string is K R P C S N Y T J M. Take alphabetic ordering of each character is worth saving time to solve these questions. Therefore,
K will be inserted at index (11-1+1)%10 = 1
R at index (18-1+1) %10 = 8
P at index (16-1+1) % 10 = 6
C at index (3-1+1) % 10 = 3
S at index (19-1+1) % 10 = 9
N at index (14-1+1) % 10 = 4
Y at index (25-1+1) %10 = 5
T at index (20-1+1) % 10 = 0
J at index (10-1+1) %10 = 0 (Ist collision occurs, because 0th index is already filled by T).
M at index (13-1+1) % 10 = 3 (2nd collision occurs, because 3rd index is already filled by C).
Only J and M causes collision.
Final Hash table will be:
0 |
T |
1 |
K |
2 |
J |
3 |
C |
4 |
N |
5 |
Y |
6 |
P |
7 |
M |
8 |
R |
9 |
S |