Data Structure (2130702)

BE | Semester-3   Summer-2018 | 05/21/2018

Q5) (b)

Explain two hash functions.

Division-Method

  • In this method we use modular arithmetic system to divide the key value by some integer divisor m (may be table size).
  • It gives us the location value, where the element can be placed.
  • We can write, L = (K mod m) + 1,
    • L = location in table/file
    • K = key value
    • m = table size/number of slots in file
  • Suppose, k = 23, m = 10 then
    • L = (23 mod 10) + 1= 3 + 1=4
    • The key whose value is 23 is placed in 4th location.

Midsquare Methods

  • In this case, we square the value of a key and take the number of digits required to form an address, from the middle position of squared value.
  • Suppose a key value is 16
    • Its square is 256
    • Now if we want address of two digits
    • We select the address as 56 (i.e. two digits starting from middle of 256)