Quadratic probing uses a hash function of the form:
h(k, i)= (h'(k) + c1i + c2i2)
mod m
Where
h'
is the auxiliary hash function,
c1
and
c2 != 0
.The initial position probed is
T(h'(k));
later positions probed are offset by amounts that depend in a
quadratic manner on the probe number
i
.
Let's take an example. Consider the keys 76, 26, 37, 59, 21, and 65 into the hash table of size m=11 using quadratic probing with c1=1 and c2=3 with hash function h'(k)=k mod m.
Initially, the table is empty.
a[0] | a[1] | a[2] | a[3] | a[4] | a[5] | a[6] | a[7] | a[8] | a[9] | a[10] |
---|---|---|---|---|---|---|---|---|---|---|
h(76,0) = (76 mod 11 + 0 + 3x0) mod 11 =10
h(26,0) = (26 mod 11 + 0 + 3x0) mod 11=4
h(59,0) = (59 mod 11 + 0 + 3x0) mod 11=4. a[4] is not free. So the next sequence will be
h(59,1) = (59 mod 11 + 1 + 3x1) mod 11=8
Similarly, other keys are placed properly in the correct position. The hash table becomes,
a[0] | a[1] | a[2] | a[3] | a[4] | a[5] | a[6] | a[7] | a[8] | a[9] | a[10] |
---|---|---|---|---|---|---|---|---|---|---|
65 | 21 | 26 | 59 | 37 | 76 |
Source code of Hashing with Quadratic Probing
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.