Monday, October 12, 2009

Modulo-Division Method

Also known as division remainder, the modulo-division method divides the key by array size (table size) and uses the remainder for the address. This method gives us the simple hashing algorithm shown below where tableSize is the number of elements in the array.

address = key MODULUS tableSize

This algorithm works with any table size, but a table size that is a prime number produces fewer collisions than other table sizes. We should therefore try, whenever possible, to make the array size a prime number.

As the little company begins to grow, we realize that soon we will have more than 100 employees. Planning for the future, we create a new employee numbering system that will handle 1,000,000 employees. We also decide that we want to provide data space for up to 300 employees. The first prime number greater than 300 is 307. We therefore choose 307 as out list (array) size, which gives us a table with addresses that range from 0 through 306. Our new employee table and some of its hashed addresses are shown in Figure 6.

To demonstrate, let’s hash Bryan Devaux’s employee number, 121267.


121267/307 = 395 with remainder of 2

Therefore: hash(121267) = 2

6 comments:

  1. In few books its given we have to add remainder +1 i.e 2+1 =3 as address

    ReplyDelete
    Replies
    1. check this out ajith
      http://note-for-it.blogspot.in/2008/11/algorithms-data-structures-hashing.html

      Delete
    2. This comment has been removed by the author.

      Delete
  2. Thanks a lot. Really helpful :)

    ReplyDelete
  3. Thanks a lot. Really helpful :)

    ReplyDelete