In: Computer Science
Linear probing:
28 |
13 |
23 |
25 |
11 |
5 |
18 |
For 5, 5%7=5, so for 5, it is inserted in 6th row(for value 0, it is 1st row, for value 2 it is 2nd row and so on).
11%7=4, it is inserted in 5th row.
18%7=4, so for 4, it should be inserted in 5th row, but already value 11 is present in that slot, hence as per linear probing if there is a collision, the key value should be inserted into next free slot. Therefore value 18 is inserted in 7th row which is the next free slot.
23%7=2, it is inserted in 3rd row.
28%7=0, it is inserted in 1st row.
13%7=6, it should be inserted in 7th row. But there is a collision, hence the next available free slot is in 2nd row.
25%7=4, it should be inserted in 5th row. But there is a collision, hence the next available free slot is in 4th row.
Quadratic probing:
28 |
18 |
23 |
11 |
5 |
13 |
For 5, 5%7=5, so for 5, it is inserted in 6th row(for value 0, it is 1st row, for value 2 it is 2nd row and so on).
11%7=4, it is inserted in 5th row.
18%7=4, so for 4, it should be inserted in 5th row, but already value 11 is present in that slot. As per quadratic probing if there is a collision, then (key + 1*1)%7 hash function is used to find the next free slot.
Therefore ( 18+1)%7 =5, it should be inserted in 6th row. But already value 5 is present in that slot. Hence the next hash function used to compute the next slot is (key + 2*2)%7= (18+4)%7=1. Therefore 18 is inserted in 2nd row.
23%7=2, it is inserted in 3rd row.
28%7=0, it is inserted in first row.
13%7=6, it is inserted in 7th row.
25%7=4, it should be inserted in 5th row, but that slot is already occupied, hence (25+1*1)%7=5 , 6th row is the next slot to be inserted, but it also occupied. (25+2*2)%7= 1, 2nd row is occupied. (25+3*3)%7=34%7= 6, it is also occupied. (25+4*4)%7=41%7= 6, (25+5*5)%7= 1, it is occupied, (25+6*6)%7=61%7= 5, it is also occupied, (25+7*7)%7=74%7=4, it is occupied. (25+8*8)%7=89%7= 5, it is already occupied. (25+9*9)%7=106%7=1, it is already occupied. (25+10*10)%7=125%7=6, it is already occupied.
Hence it goes on and it can't find an empty slot for 25 in quadratic probing.
chaining:
Each slot is made to point to linked list of records that have same hash value in the chaining method.
18%7=4, 5th row is already occupied, hence a separate chain for the value 18 is made so that value 11 which is already occupied in that slot is made to point that new value 18.
When 25 also has to be inserted in 5th row, value 18 is made to point to this new value 25. Hence a
linked list of values that maps to same hash value is formed.