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) = x \\mod 16"
Insertion algorithm
For linear probing probe function is "\ud835\udc5d(\ud835\udc65,\ud835\udc56)=(\u210e(\ud835\udc65)+\ud835\udc56 )\\mod \ud835\udc46"
Hash Table
"\\begin{matrix}\n \\bold{Value} & -\t&1&\t2&\t17&\t36&\t5&\t49&\t-&\t-&\t25&\t10&\t-&\t-&\t-&\t-&\t- \\\\\n \\bold{Index} & 0&\t1&\t2&\t3&\t4&\t5&\t6&\t7&\t8&\t9&\t10&\t11&\t12&\t13&\t14&\t15\n\\end{matrix}"
Explanation:
For "1,2,5,10,25,36" insert value at index "\u210e(\ud835\udc65)"
For "17" cell with index "\u210e(17)=1" already filled.
Continuing to the adjacent cells:
"\ud835\udc5d(17,1)=(\u210e(17)+1) \\mod \ud835\udc46=2" -- also filled,
"\ud835\udc5d(17,2)=(\u210e(17)+2) \\mod \ud835\udc46=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:
for some choice of constants "c1, \ud835\udc502, \ud835\udc503". For "S=2^n" , a good choice for the constants are "\ud835\udc50_1=\ud835\udc50_2=\\frac{1}{2}" as the values of "\ud835\udc5d(x,\ud835\udc56)" for "\ud835\udc56" in "[0,\ud835\udc46\u22121]" are all distinct. This leads to a probe sequence of
the triangular numbers.
In our case we have "\ud835\udc46=16=2^4" , so let's select "\ud835\udc50_1=\ud835\udc50_2=\\frac{1}{2} ." So we have probe function:
Hash Table
"\\begin{matrix}\n \\bold{Value} & -&\t1&\t2&\t-&\t36&\t5&\t-&\t17&\t-&\t25&\t10&\t49&\t-&\t-&\t-&\t- \\\\\n \\bold{Index} & 0&\t1&\t2&\t3&\t4&\t5&\t6&\t7&\t8&\t9&\t10&\t11&\t12&\t13&\t14&\t15\n\\end{matrix}"Explanation:
For "1,2,5,10,25,36" insert value at index "h(x)".
For "17" cell with index "\u210e(17)=1" already filled.
Continuing to the adjacent cells:
"\ud835\udc5d(17,1)=2 \\\\\n\ud835\udc5d(17,2)=4 \\\\\n\ud835\udc5d(17,3)=7" -- empty fill it with 17.
For "49" same approach as for "17".
"\ud835\udc5d(49,4)=11"
Comments
Leave a comment