Insert {1, 2, 5, 10, 17, 25, 36, 49} into empty hash table with Table Size: S=16
Once, we have a hash table with S=16 , so hash function h(x) for integers is: h(x)=xmod16
xh(x)1122551010171259364491
Insertion algorithm
- Start at the cell at index h(x) and continuing to the adjacent cells h(x)+1,h(x)+2,…,h(x)+k until finding an empty cell.
- insert in this empty cell.
For linear probing probe function is p(x,i)=(h(x)+i)modS
Hash Table
ValueIndex−0112217336455496−7−82591010−11−12−13−14−15
Explanation:
For 1,2,5,10,25,36 insert value at index h(x)
For 17 cell with index h(17)=1 already filled.
Continuing to the adjacent cells:
p(17,1)=(h(17)+1)modS=2 -- also filled,
p(17,2)=(h(17)+2)modS=3 -- empty, fill it with 17
For 49 same approach as for 17 .
The Insertion algorithm is similar to Linear probing, only difference -- different probe function.
The probe function is some quadratic function:
p(x,i)=(h(x)+c1i2+c2i+c3)modS
for some choice of constants c1,c2,c3. For S=2n , a good choice for the constants are c1=c2=21 as the values of p(x,i) for i in [0,S−1] are all distinct. This leads to a probe sequence of
h(x),h(x)+1,h(x)+3,h(x)+6,…the triangular numbers.
In our case we have S=16=24 , so let's select c1=c2=21. So we have probe function:
p(x,i)=(h(x)+21i2+21i)modS Hash Table
ValueIndex−01122−336455−6177−825910104911−12−13−14−15Explanation:
For 1,2,5,10,25,36 insert value at index h(x).
For 17 cell with index h(17)=1 already filled.
Continuing to the adjacent cells:
p(17,1)=2p(17,2)=4p(17,3)=7 -- empty fill it with 17.
For 49 same approach as for 17.
p(49,4)=11
Comments