×

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique.

Hash table

We can understand the hash table better based on the following points:

  1. In a data structure, the hash table is used to store key-value pairs.
  2. The data items are stored in an array format in it, where each data value has its unique index value.
  3. It is a collection of stored data items that you can easily search later.
  4. In this, the hash function is used to compute the index of the array, from which you can find your desired value.
  5. It has a list of the array where each list is called a bucket.
  6. It contains the value based on the key.
  7. The hash table is used to implement the map interface and extend the dictionary class.
  8. The hash table is a synchronized table, and it contains unique elements only.
Hashing
  • The figure above shows the hash table whose size is n = 10. Each position of the hash table is called a slot. This hash table has not contained any item, so each slot is empty.

Basic operations

There is the following type of basic operation in the hash table:

  1. Search operation
  2. Delete operation
  3. Insert operation

Search operation: The search operation is used to search a particular value in a hash table.

Delete operation: The delete operation is used to delete a particular value in a hash table.

Insert operation: The Insert operation is used to add particular value in a hash table.

Hash function

The hash function is a type of function that is applied to a key from which an integer is obtained. This integer is used as the address of the hash table. This integer is called a hash key.

Type of hash functions

There are various types of hash functions in the data structure:

  1. Mid Square Hash Function
  2. Division Hash Function

Division hash function

In this function, the hash function depends on the remainder of the division. If the list size is a prime number, there is less collision.

The formula of division hash function is h(k) = k mod n

Where k is key, and n is list size.

For example: Suppose we have this record (101, 103, 107, 109), and the table size is 10.

Solution: Record is (101, 103, 107, 109)

                Table size is 10.

                Put value in the formula h(k) = k mod n

                                        1 = 101 mod 10

                                        3 = 103 mod 10

                                        7 = 107 mod 10

                                        9 = 109 mod 10

Hashing

Mid square hash function

In this function, firstly hash function key is squared, and then the middle part of the square is selected as the index.

For example: Suppose we have this record 96.

                        96 = 962 = 9216                              // middle part of the square is index address    

                        The address of index is 21 

Characteristics of a good hash function

To get a good hashing mechanism, we need a good hash function. Therefore, we describe below the characteristic of a good hash function:

  1. The hash function should generate different hash values ??for the same type of string.
  2. A good hash function reduces the chance of collisions.
  3. It should be easy to compute.
  4. The hash function is perfect when it uses all input data.
  5. The hash function should generate such keys that are easy to distribute.

Related Topics

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.

Convert Sorted List to Binary Search Tree

Implementation // creating the C++ implementation of the following approach: - #include <bits/stdc++.h> using namespace std; /* Create the link list node and see its implementation. */ class L__Nod { public: int record; L__Nod* next; }; /* constructing a new binary...

15 minutes read.

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

3 minutes read.

Asymptotic Notation

Asymptotic notation is expressions that are used to represent the complexity of algorithms. The complexity of the algorithm is analyzed from two perspectives:  Time complexitySpace complexity Time complexity The time complexity of an algorithm is the...

3 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Timsort

TimSort Time Complexity Timsort is a sorting algorithm that is quite efficient for real-world data. Timsort is created in 2001 by Tim Peters for the python programming language. Timsort is a...

3 minutes read.

About Data Structures

What exactly are data structures? A data structure is a type of storage that is used to organise and store data. It is a method of organising data on a computer...

5 minutes read.

Symmetric binary tree

Implementation // writing a C++ program to check whether a given binary tree is symmetric or not. #include <bits/stdc++.h> using namespace std; // creating a binary tree node. struct __Nod { int ky; struct __Nod *Lft, *Rt; }; //...

4 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Intersection Point in Y Shaped Linked Lists in Java

Intersection Point in Y Shaped Linked Lists in Java In this article, we are going to see how to find the intersection point in a Y-shaped linked list. Method 1: We need to...

4 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.

Recursion in Fibonacci

Fibonacci heap is considered to be a particular execution of the heap data structure that ultimately helps in making use of not just any number but the Fibonacci numbers. It...

3 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

11 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Bubble Sort vs Merge Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Merge Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Bubble Sort in Data Structures

Bubble Sort in C++ The bubble sort algorithm analyses two adjacent elements and swaps them until they are no longer in the desired order. Each iteration moves each member of the array...

4 minutes read.