algorithmhashtablequadratic-probing

Hash table quadrtc. probing


Need an example

I need to Give the size of the table and the elements I have tried to insert, which I could not insert because of collision after the table is more than half full.

I have tried a few different inputs on the table size, with the function: h of i (x) = (hash(x) + f(i)) mod table size

f(i)= i^2

I would appreciate any help thanks.


Solution

  • For a table of size 7 assume the spots 0, 1, 2, 4 are taken, 3, 5, 6 are free. Now try to insert an x where hash(x) = 0.

    Example: lets take hash(x) = x % 7.

    Insert 0: hash(0) = 0, this slot is free so insert 0 into slot 0
    Insert 1: hash(1) = 1, this slot is free so insert 1 into slot 1
    Insert 2: hash(2) = 2, this slot is free so insert 2 into slot 2
    Insert 4: hash(4) = 4, this slot is free so insert 4 into slot 4
    Insert 7: hash(7) = 0, slot 0 is already taken; start quadratic probing:
    (0+1*1)%7 = 1 also taken
    (0+2*2)%7 = 4 also taken
    (0+3*3)%7 = 2 also taken
    (0+4*4)%7 = 2 also taken
    (0+5*5)%7 = 4 also taken
    (0+6*6)%7 = 1 also taken
    ...