Answer to Question #124455 in Algorithms for David

Question #124455
Insert {1, 2, 5, 10, 17, 25, 36, 49} into empty hash table with Table Size = 16 using
a. Linear Probing
b. Quadratic Probing
1
Expert's answer
2020-06-30T15:55:16-0400

Task:

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"


"\\begin{matrix}\n \\bold{x} & 1 & 2 &\t5&\t10&\t17&\t25&\t36&\t49 \\\\\n \\bold{h(x)} & 1\t&2&\t5&\t10&\t1&\t9&\t4&\t1\n\\end{matrix}"



a) Linear probing

Insertion algorithm

  1. Start at the cell at index "h(x)" and continuing to the adjacent cells "\u210e(\ud835\udc65)+1, \u210e(\ud835\udc65)+2, \\dots ,\u210e(\ud835\udc65)+\ud835\udc58" until finding an empty cell.
  2. insert in this empty cell.

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" .

b) Quadratic probing

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)+c_1i^2+c_2i+c_3) \\mod S"


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


"\u210e(\ud835\udc65),\u210e(\ud835\udc65)+1,\u210e(\ud835\udc65)+3,\u210e(\ud835\udc65)+6,\\dots"

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:


"\ud835\udc5d(\ud835\udc65,\ud835\udc56)= \\left( \u210e(\ud835\udc65)+\\frac{1}{2}\ud835\udc56^2+\\frac{1}{2}\ud835\udc56 \\right) \\mod \ud835\udc46"

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"


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS