Tuesday 10 July 2012

Hash Function : Division-Remainder Method


Map a key k into one of m slots by taking the remainder of k divided by m. That is, the hash function is h (k) = k mod m.

In this method, if 5,000 records then divide by 5000. The remainder will be between 0 and 4999. 

Scenario 1-

If the number of records increases then two key values will land on same address. E.g. 
  • When Key=1, then Key mod 5000 = 1
  • When Key=5001 then Key mod 5000 = 1
Hence depending on key distribution this method may fail.

Scenario 2-

In the college 3 digits are assigned for roll number for the students of each class. The roll numbers can be from 000 to 999, hence divide by 1000. When you divide roll number 1 by 1000 then remainder will be 1 (this is the address where the record of roll number 1 is stored). But roll number 1 is in FY, SY and TY. Hence there is a collision as roll number 1 of all the classes will refer to the same address on the disk. Solution to this problem can be assign unique roll numbers. If last assignment was 199 then the next admission will get roll number 200 irrespective of the class. But this will get exhausted at 999. 

It works fine with keys that are sparse and with any list size, but a list size that is a prime number produces fewer collisions than other list sizes.

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.

Do you like this article?