0% found this document useful (0 votes)
13 views8 pages

20.hashing Search Technique

Uploaded by

hackerktp2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

20.hashing Search Technique

Uploaded by

hackerktp2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Searching Techniques-

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-

The main drawback of these techniques is-


 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 in Data Structure-

In Data Structures,
 Hashing is a well-known technique to search any particular element among
several elements.

 It minimizes the number of comparisons while performing the search.

Advantage-

Unlike other Searching techniques,


 Hashing is extremely efficient.
 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).

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.

Hashing is the technique/ process of mapping key: value pairs by calculating a


Hash code using the Hash Function. When given a (key: value) pair, the Hash
Function calculates a small integer value from the key.
The obtained integer is called the Hash value/ Hash code and acts as the index to
store the corresponding value inside the Hash Table.

If for two (key: value) pairs, the same index is obtained after applying the Hash
Function, this condition is called Collision.

We need to choose a Hash Function such that Collision doesn't occur.

Hash Key Value-

 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.

Properties of Hash Function-

The properties of a good hash function are-


 It is efficiently computable.
 It minimizes the number of collisions.
 It distributes the keys uniformly over the table.
Types of Hash Functions-

There are various types of Hash Functions available such as-


1. Division Method
2. Mid Square Method
3. Digit Folding Method

It depends on the user which hash function he wants to use.

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.

The hash function is dependent upon the remainder of a division.

Example:

k = 12345
M = 95
h(12345) = 12345 mod 95
= 90
k = 1276
M = 11
h(1276) = 1276 mod 11
=0

Advantages

1. This method is quite good for any value of M.


2. The Division Method is very fast since it requires only a single division
operation.

Drawbacks

1. This method leads to poor performance since consecutive keys map to


consecutive Hash Values in the Hash Table.
2. Sometimes extra care should be taken to choose the value of M.

Mid - Square Method

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.

2. The result is not dominated by the distribution of the top digit or


bottom digit of the original key value.

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.

2. Another disadvantage is that there will be collisions but we can try to


reduce collisions.
Digit Folding Method

This method involves two steps:

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:

k = k1, k2, k3, k4, ….., kn


s = k1+ k2 + k3 + k4 +….+ kn
h(K)= s
Here,
s is obtained by adding the parts of the key k

Example:

k = 12345
k1 = 12, k2 = 34, k3 = 5
s = k1 + k2 + k3
= 12 + 34 + 5
= 51
h(K) = 51

You might also like