Hashing Techniques - U3
Hashing Techniques - U3
Introduction
There are several searching techniques like linear search, binary search, search trees etc. In
these techniques, time taken to search any element depends on the total number of elements.
Linear Search takes O(n) time to perform the search in unsorted arrays consisting of n elements.
Binary Search takes O(logn) time to perform the search in sorted arrays consisting of n elements.
It takes O(logn) time to perform the search in Binary Search Tree consisting of n elements. The
main drawback of these techniques are:
• As the number of elements increases, time taken to perform the search also increases.
• This becomes problematic when total number of elements become too large.
Hashing
Hashing is a technique to search an element ‘x’ among several elements. It minimizes the
number of comparisons while performing the search. The time taken by it to perform the search
does not depend upon the total number of elements. It completes the search with constant time
complexity O(1).
An array data structure called as Hash table is used to store the data items. Based on the hash
key value, data items are inserted into the hash table. Hash key value is a special value that
serves as an index for a data item. It indicates where the data item should be stored in the hash
table. Hash key value is generated using a hash function.
Hash function is a function that maps any big number or string to a small integer value. Hash
function takes the data item as an input and returns a small integer value as an output. The small
integer value is called as a hash value. Hash value of the data item is then used as an index for
storing it into the hash table.
There are various types of hash functions available such as: Mid Square Hash Function, Division
Hash Function and Folding Hash Function etc.
1. Using the hash function ‘key mod 7’, insert the following sequence of keys in the hash table-
50, 700, 76, 85, 92, 73 and 101. Use separate chaining technique for collision resolution.
Solution
The given sequence of keys will be inserted in the hash table as-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Step-07:
Step-08:
Open Addressing:
In open addressing, all the keys are stored inside the hash table. No key is stored outside the hash table.
Techniques used for open addressing are-
• Linear Probing
• Quadratic Probing
• Double Hashing
Insert Operation
• Hash function is used to compute the hash value for a key to be inserted.
• Hash value is then used as an index to store the key in the hash table.
In case of collision,
• Probing is performed until an empty bucket is found.
• Once an empty bucket is found, the key is inserted.
• Probing is performed in accordance with the technique used for open addressing.
Search Operation
To search any particular key,
• Its hash value is obtained using the hash function used.
• Using the hash value, that bucket of the hash table is checked.
• If the required key is found, the key is searched.
• Otherwise, the subsequent buckets are checked until the required key or an empty bucket is
found.
• The empty bucket indicates that the key is not present in the hash table.
Delete Operation
• The key is first searched and then deleted.
• After deleting the key, that particular bucket is marked as “deleted”.
2. Quadratic Probing
In quadratic probing, as the collision occurs, probe for i2‘th bucket in ith iteration. Keep probing until
an empty bucket is found.
3. Double Hashing
In double hashing, another hash function hash2(x) is used and look for i * hash2(x) bucket in ith
iteration. It requires more computation time as two hash functions need to be computed.
In open addressing, the value of load factor always lie between 0 and 1.
This is because-
• In open addressing, all the keys are stored inside the hash table.
• So, size of the table is always greater or at least equal to the number of keys stored in the
table.
Using the hash function ‘key mod 7’, insert the following sequence of keys in the hash table:
50, 700, 76, 85, 92, 73 and 101, Use linear probing technique for collision resolution.
Solution-
The given sequence of keys will be inserted in the hash table as-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Step-07:
Step-08:
• The next key to be inserted in the hash table = 101.
• Bucket of the hash table to which key 101 maps = 101 mod 7 = 3.
• Since bucket-3 is already occupied, so collision occurs.
• To handle the collision, linear probing technique keeps probing
linearly until an empty bucket is found.
• The first empty bucket is bucket-5.
• So, key 101 will be inserted in bucket-5 of the hash table as-
Separate Chaining
Separate Chaining is advantageous when it is required to perform all the following operations
on the keys stored in the hash table-
• Insertion Operation
• Deletion Operation
• Searching Operation
• Deletion is easier in separate chaining.
• This is because deleting a key from the hash table does not affect the other keys stored in
the hash table.
Open Addressing
Open addressing is advantageous when it is required to perform only the following operations
on the keys stored in the hash table-
• Insertion Operation
• Searching Operation
• Deletion is difficult in open addressing.
• This is because deleting a key from the hash table requires some extra efforts.
• After deleting a key, certain keys must be rearranged.
_________________________