HASHING
HASHING
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