Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Hash Functions and Hash Tables
Hashing is the process of generating a value from a text or a list of numbers using a mathematical function known as a hash function. Hash functions convert input data of arbitrary size into fixed-size hash values, which are used as indices in hash tables for efficient data storage and retrieval.
Hash Functions
Hash functions are algorithms that map data to fixed-size hash values. Here are the most commonly used hash functions −
Division Method
This is the simplest method to create a hash function. The hash function can be described as −
h(k) = k mod n
Here, h(k) is the hash value obtained by dividing the key value k by size of hash table n using the remainder. It is best that n is a prime number as that ensures the keys are distributed with more uniformity.
Example:
k = 1276 n = 10 h(1276) = 1276 mod 10 = 6
The hash value obtained is 6.
A disadvantage of the division method is that consecutive keys map to consecutive hash values in the hash table, leading to poor performance due to clustering.
Multiplication Method
The hash function used for the multiplication method is −
h(k) = floor( n × (kA mod 1) )
Here, k is the key and A can be any constant value between 0 and 1. Both k and A are multiplied and their fractional part is separated. This is then multiplied with n to get the hash value.
Example:
k = 123
n = 100
A = 0.618033
h(123) = 100 × (123 × 0.618033 mod 1)
= 100 × (76.018059 mod 1)
= 100 × (0.018059)
= 1
The hash value obtained is 1.
An advantage of the multiplication method is that it can work with any value of A, although some values (like the golden ratio ? 0.618) are believed to be better than others.
Mid Square Method
The mid square method involves squaring the value of the key and then extracting the middle r digits as the hash value. The value of r is decided according to the size of the hash table.
Example:
Suppose the hash table has 100 memory locations. So r = 2 because two digits are required to map the key to memory location.
k = 50 k² = 2500 h(50) = 50 (middle 2 digits of 2500)
The hash value obtained is 50.
Hash Tables
A hash table is a data structure that maps keys to values using a hash function to calculate the index where data should be stored. It provides average-case O(1) time complexity for search, insertion, and deletion operations.
Advantages of Hash Tables
Fast Access − Average O(1) time complexity for search, insert, and delete operations.
Flexible Keys − Can use various data types as keys (strings, numbers, objects).
Dynamic Size − Can grow or shrink as needed with proper resizing strategies.
Collision Resolution
When two keys hash to the same index, a collision occurs. Common resolution methods include −
Linear Probing − Search for the next available slot sequentially.
Chaining − Store multiple values at the same index using linked lists.
Quadratic Probing − Use quadratic function to find the next available slot.
Conclusion
Hash functions and
