Types of Hash Functions in C17 Mar 2025 | 5 min read 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. Terminology:
This article explains different types of Hash Functions programmers frequently use. These are the four Hash Functions we can choose based on the key being numeric or alphanumeric:
1. Division Method:Say that we have a Hash Table of size 'S', and we want to store a (key, value) pair in the Hash Table. The Hash Function, according to the Division method, would be:
Let us now take an example to understand the cons of this method: Size of the Hash Table = 5 (M, S) Key: Value pairs: {10: "Sudha", 11: "Venkat", 12: "Jeevani"} For every pair:
Observe that the Hash values were consecutive. This is the disadvantage of this type of Hash Function. We get consecutive indexes for consecutive keys, leading to poor performance due to decreased security. Sometimes, we need to analyze many consequences while choosing the Hash Table size. A simple program to demonstrate the mechanism of the division method:Output: Enter the size of the Hash Table: 5 The indexes of the values in the Hash Table: 0 1 2 2. Mid Square Method:It is a two-step process of computing the Hash value. Given a {key: value} pair, the Hash Function would be calculated by:
We should choose the number of digits to extract based on the size of the Hash Table. Suppose the Hash Table size is 100; indexes will range from 0 to 99. Hence, we should select 2 digits from the middle. Suppose the size of the Hash Table is 10 and the key: value pairs are: {10: "Sudha, 11: "Venkat", 12: "Jeevani"} Number of digits to be selected: Indexes: (0 - 9), so 1 H(10) = 10 * 10 = 100 = 0 H(11) = 11 * 11 = 121 = 2 H(12) = 12 * 12 = 144 = 4
A simple program to demonstrate the mechanism of the mid-square method:3. Folding MethodGiven a {key: value} pair and the table size is 100 (0 - 99 indexes), the key is broken down into 2 segments each except the last segment. The last segment can have less number of digits. Now, the Hash Function would be:
For suppose "k" is a 10-digit key and the size of the table is 100(0 - 99), k is divided into: sum = (k1k2) + (k3k4) + (k5k6) + (k7k8) + (k9k10) Now, H(x) = sum % 100 Let us now take an example: The {key: value} pairs: {1234: "Sudha", 5678: "Venkat"} Size of the table: 100 (0 - 99) For {1234: "Sudha"}: 1234 = 12 + 34 = 46 46 % 100 = 46 For {5678: "Venkat"}: 5678 = 56 + 78 = 134 134 % 99 = 35 ![]() 4. Multiplication methodUnlike the three methods above, this method has more steps involved:
So, the Hash Function under this method will be: For example: {Key: value} pairs: {1234: "Sudha", 5678: "Venkat"} Size of the table: 100 A = 0.56 For {1234: "Sudha"}: H(1234) = floor(size(1234*0.56 mod 1)) = floor(100 * 0.04) = floor(4) = 4 For {5678: "Venkat"}: H(5678) = floor(size(5678*0.56 mod 1)) = floor(99 * 0.68) = floor(67.32) = 67 ![]()
What after computing the Hash value? After computing the Hash value using the hash Function, this value is used as an index in the Hash table. Whenever the user wants to access a value, the corresponding key is hashed using the Hash Function, which gives the index of the key's value in the Hash Table with less cost than regular arrays and linked lists. Hence, Hashing is used to reduce the Time as well as space complexity of the program. |
Introduction One frequently runs into issues with handling large amounts of data and effectively performing range queries in the world of data structures and algorithms. An elegant and effective data structure called the "" offers a solution to this kind of issue. In this article, we will...
6 min read
The word LIFO stands for Last In First Out, in which we will enter the data elements into the data structure. Here, we will pop out the data elements which are recently added. It means that the last element will be the first to be popped...
16 min read
A height-balanced binary tree generally appears to be the one in which the right subtree and the left subtree of any given node do not change or predicate by more than one, and also it focuses on the fact that both the subtrees are height-balanced. To identify...
7 min read
Introduction In computer science and mathematics, matrices are fundamental structures that are used as the building blocks of different algorithms and computations. Different matrix manipulation techniques can produce intriguing patterns and effective solutions. Printing a matrix in spiral form is one such fascinating process.When we refer to...
4 min read
Problem Statement: Given a balanced (height balanced) binary search tree and the task is to find whether there exists a triplet (3 elements) that sum up to 0 and if present return present else not present Input: 6 / \ -13...
7 min read
Merging two sorted arrays is a popular procedure in computer science. The difficulty emerges when you are charged with combining these arrays in place with no extra space allocation. This issue frequently arises in interviews and real-world circumstances when memory is a crucial restriction. Let's look...
9 min read
Trees are essential structures with a wide range of applications in the large field of data structures and computer science. The Kth Ancestor Problem in Trees is one fascinating issue that has drawn interest. The Kth Ancestor Problem, which has applications in network routing, hierarchical data...
6 min read
This article will provide an overview and Python implementation of the merge two sorted linked lists algorithm. Linked lists are fundamental data structures used in computer science and programming. They provide an efficient way to store and organize data non-contiguously. Linked lists consist of nodes containing data...
4 min read
In this article, we will discuss how to implement Quick Sort using Hoare's Partition, its applications, and the advantages of Hoare's Partition scheme over the Lomuto partition scheme. Quick Sort The idea of this sorting algorithm is to select an element (pivot element) and find its correct position...
13 min read
. Runtime efficiency is critical for scalability when designing data structures to support essential operations like inserting, deleting, searching, and retrieving elements. As data structures grow to hold more and more details, keeping these operations fast becomes challenging. While primary data structures like arrays or linked...
8 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India