Non-Linear Data Structure 553
15.8 HASHING TECHNOLOGY
Hash method: Hash technique is one of the complex searching techniques. In this topic, we consider
a class of search techniques whose search time is dependent on the number of entries available in the
table. In this method, we fix the position of the key (element) into the table or the file, which is determined
by the hash function. The function in which we use this key is known as the hashing function.
For example, if we want to search a number from the ten numbers of a file, then we must find the
number throughout this range from the first number to the tenth number. When we use the key for
fixing its position in a table, then the number can be searched very easily.
Hashing function: In order to follow the operation of a hashing function consider an array that
comprises a list containing some finite number of elements. Each element of it is a key. On performing
operations on each key, some value is obtained. The key is assigned to the index, which is having the
appropriate value. The table prepared with the keys is called a hashing table.
Let us assume the following array that will hold the hash table.
12,22,32,17,19,28,15,29,16,18,13,11.
Let us use the division method for the following hash table. We divide the key by '3' and the key is
placed in the index based on the remainders obtained. The keys having equal remainders are placed
in the same index. Any number divided by 3 always returns 0,1 or 2. A hash table is prepared with
three indices such as 0,1,2. All the above keys are stored in these three indices as shown in Table 15.1.
Take the first key '12', divide it by the '3'. The remainder is 'O', hence insert it into the '0' index.
Then take the next key '22', divide it by the '3'. The remainder is T . Hence, insert the key in to the T
index.
In the same way, divide the key '32' by 3. Its reminder is 2 after dividing it by 3. Place it into the '2'
index in the table.
For the remaining elements, the same procedure is to be repeated and by knowing reminders the
numbers can be placed appropriately in the respective index table.
Table 15.1 explains a hashing table.
Table 15.1 Hash table
Index Keys
0 12,15,18
1 22,19,28,16,13
2
32,17,29,11
In short, the hashing function takes the key, and maps it to some index to the array. In our example,
division function has been selected. The programmer can take any other function. This function maps
several different keys to the same index. In the above table, keys 12,15 and 18 are assigned to the index
0. While keys 22,19,28,16 and 13 are placed in index 1. Keys 32,17,29 and 11 are in the index 2.
After preparation of the index table, it becomes easier to search the element from the list. One can
use any searching method to search the element from the hashing table.
Consider the following program for preparation of an index using divide method.
15.3 Write a program to prepare the hashing table. Enter the elements through the keyboard
and map them in index. Use the division function.
#include<8tdio. h>
#include<canio. h>
main ()