Hash Functions
Types of Hash Functions
Hash functions play a key role in determining how efficiently a hash table works. Below are some classic hashing techniques used to compute hash values.
1. ๐ฆ Division Method
Formula:
Where:
-
k
is the key -
m
is the size of the hash table (preferably a prime number)
✅ Advantages:
-
Simple and fast
-
Easy to implement
❌ Disadvantage:
-
Poor distribution if
m
is not chosen wisely (e.g., ifm
is a power of 2)
๐งช Example:
2. ๐ฉ Mid-Square Method
Steps:
-
Square the key.
-
Extract the middle digits of the result.
-
Use those digits as the hash value or apply
% m
.
๐งช Example:
✅ Advantages:
-
Works well for small keys.
-
Middle digits are often more "randomized".
❌ Disadvantage:
-
Squaring is slower than modulo operation.
3. ๐จ Folding Method
This method divides the key into parts (usually of equal length), then adds the parts together to compute the hash.
Techniques:
-
Simple Folding: Split into parts and add.
-
Folding by Shifting: Reverse alternate parts before adding.
๐งช Example (Simple Folding):
๐งช Example (Folding by Shifting):
✅ Advantages:
-
Good for long numeric keys
-
Adds variability
❌ Disadvantage:
-
Some patterns may still result in collisions.
4. ๐ฅ Digit Analysis Method
This method analyzes specific digits of the key (e.g., only use certain positions like the 2nd and 4th digits).
Use case:
-
When we know the dataset characteristics beforehand (e.g., employee IDs).
๐งช Example:
✅ Advantages:
-
Can be efficient for structured keys
❌ Disadvantage:
-
Can lead to clustering if digits chosen are not well-distributed.
๐ Summary Table
Method | Idea | Pros | Cons |
---|---|---|---|
Division | k % m | Simple, fast | Needs good m , may cluster |
Mid-Square | Square key, take middle digits | Random distribution | Slower due to squaring |
Folding | Break into parts, add them | Handles large keys | Needs careful part selection |
Digit Analysis | Use selected digits | Fast for structured keys | Not generic; risks clustering |
Comments
Post a Comment