0% found this document useful (0 votes)
5 views9 pages

HASHING

Uploaded by

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

HASHING

Uploaded by

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

HASHING IN DATA STRUCTURE

• Hashing is a technique that is used to uniquely identify a specific objects


from a group of similar objects
• Hashing is the transformation of a string of characters into a usually
shorter fixed-length value or key that represents the original string.
• In hashing large keys are converted into small keys by using hash
functions.
• The values are then stored in a data structure called hash tables
• The task of hashing is to distribute entries(key/value) uniformly across
an array
• The element is stored in the hash table where it can be quickly
retrieved using hashed key
• Hash Key = Key Value % Number of slots in the table
Types of Hash Functions
• 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
h(K) = k mod M
Here,
k is the key value, and
M is the size of the hash table
Hashing (Storing & Retrieving data in O(1) time)
• Search Key(24,52,91,67,48,83)
• Hash Table
• Hash Function (K mod 10, K mod n, Mid-Square Method, Folding
Method)
0
K mod 10 Method 91 1
24=K 52 2
K mod 10 83 3
24 4
24%10= 4 Hash Value
5
Mid-Square Method 6
Key= 136 67 7
square of(3)=9 (hash value) 48 8
9

Hash Table
2. 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.
3. Digit Folding Method:​
This method involves two steps:​
•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.​
•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
For e.g. Key = 123456
123
+456
579 Data storage location
Hash Table in C
A Hash Table in C/C++ (Associative array) is a data structure that
maps keys to values. This uses a hash function to compute indexes
for a key.
Based on the Hash Table index, we can store the value at the
appropriate location.
If two different keys get the same index, we need to use other data
structures (buckets) to account for these collisions.
Any Hash Table implementation has the
following three components:
•A good Hash function to map keys to values
•A Hash Table Data Structure that
supports insert, search and delete operations
.
•A Data Structure to account for collision of keys

You might also like