# Hashing by Division in Data Structure

Data StructureAnalysis of AlgorithmsAlgorithms

Here we will discuss about the hashing with division. For this we use the hash function −

ℎ(𝑥) = 𝑥 𝑚𝑜𝑑 𝑚

To use this hash function we maintain an array A[0, … m – 1]. Where each element of the array is a pointer to the head of the linked list. The linked list Li is pointed to array element A[i] holds all elements x such that h(x) = i. This technique is known as hashing by chaining.

In such hash table, if we want to increase an element x, this will take O(1) time. we compute the index i = h(x). Then append or prepend x to the list Li. If we want to search or delete an element, that process is not so easy. We have to find index i = h(x). Then traverse list Li. Until we reach the desired value or the list is exhausted. This operation is taking time corresponding to the size of list Li. If our set S has 0, m, 2m, 3m, …., nm elements, then all elements stored in L0 will take linear time to search and delete.

This kind of situation is very rare. For example, if S are uniformly and independently distributed in the universal set U, and u is a multiple of m, then the expected size of each list Li is only n/m. In this case the searching and deleting takes O(1 + α) amount of time. To avoid the mentioned scenario, we have to choose m wisely. generally, we avoid m as power of 2. Taking m as a prime which is not too closer to power of 2 is recommended.

Published on 10-Aug-2020 09:22:24