20.hashing Search Technique
20.hashing Search Technique
In Data Structures,
There are several searching techniques like Linear Search, Binary Search, Search
Trees etc.
In these techniques, time taken to search any particular element depends on
the total number of elements.
Example-
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.
Drawback-
This becomes problematic when total number of elements become too large.
Hashing in Data Structure-
In Data Structures,
Hashing is a well-known technique to search any particular element among
several elements.
Advantage-
Hashing Mechanism-
In Hashing,
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.
If for two (key: value) pairs, the same index is obtained after applying the Hash
Function, this condition is called Collision.
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-
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.
Division Method
This is the most simple and easiest method to generate a hash value. The hash
function divides the value k by M and then uses the remainder obtained.
Formula:
h(K) = k mod M
Here,
k is the key value, and
M is the size of the hash table.
It is best suited that M is a prime number as that can make sure the keys are
more uniformly distributed.
Example:
k = 12345
M = 95
h(12345) = 12345 mod 95
= 90
k = 1276
M = 11
h(1276) = 1276 mod 11
=0
Advantages
Drawbacks
The Mid-Square Method is a very good hashing method. It involves two steps to
compute the hash value-
1. Square the value of the key k i.e. k2
2. Extract the middle r digits as the hash value.
Formula:
h(K) = h(k x k)
Here,
k is the key value.
The value of r can be decided based on the size of the table.
Example:
Suppose the Hash Table has 100 memory locations. So, r = 2 because two digits
are required to map the key to the memory location.
k = 60
k x k = 60 x 60
= 3600
h(60) = 60
The hash value obtained is 60
Advantages
1. The performance of this method is good as most or all digits of the key
value contribute to the result. This is because all digits in the key
contribute to generating the middle digits of the squared result.
Drawbacks
1. The size of the key is one of the limitations of this method, as the key is
of big size then its square will double the number of digits.
1. Divide the key-value k into a number of parts i.e. k1, k2, k3,….,kn,
where each part has the same number of digits except for the last part
that can have lesser digits than the other parts.
2. Add the individual parts. The hash value is obtained by ignoring the last
carry if any.
Formula:
Example:
k = 12345
k1 = 12, k2 = 34, k3 = 5
s = k1 + k2 + k3
= 12 + 34 + 5
= 51
h(K) = 51