Implement Hash Tables with Quadratic Probing in C++



Hash Table

A hash table is a data structure which is used to store key-value pairs and uses hash function to compute an index into an array of buckets or slots in which an element will be inserted or searched in average-case constant time.

Why are Collision a Problem?

A collision is a problem because even a perfect hash function can generate the same index for multiple keys, causing a collision. To resolve these, we use techniques like ?

  • Chaining
  • Linear Probing
  • Quadratic Probing

Quadratic Probing

Quadratic probing is a collision-resolving technique in open-addressed hash tables. It operates by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until an open slot is found using the below formula.

(index + i²) % table_size
// where i is the number of attempts. 

Algorithm

Algorithm of hash table with Quadratic Probing ?

    Step 1: Initialise Table

  • Create a fixed-size array table[] of size TABLE_SIZE.
  • Set all elements to a special value (e.g., -1) indicating empty slots.
  • Step 2: Hash Function

  • Compute the initial index using:
index = key % TABLE_SIZE

    Step 3: Insert Key

  • set i = 0. While i < table_size. Compute the prob index using:
newIndex = (index + i^2) % TABLE_SIZE
  • If table[newIndex] is empty: Insert the key at table[newIndex]. Stop.
  • Else: Increment i.
  • If no empty slot is found: Report that the hash table is full.
  • Step 4: Display Table

  • For each index from 0 to TABLE_SIZE - 1: Print the index and the value stored at that index.

Example: Hash Table with Quadratic Probing

The following is a simple implementation of the hash tables with quadratic probing using the above algorithm ?

#include <iostream>
using namespace std;

const int TABLE_SIZE = 10;

class HashTable {
   int* table;

public:
   HashTable() {
      table = new int[TABLE_SIZE];
      for (int i = 0; i < TABLE_SIZE; i++)
         table[i] = -1;  // -1 indicates an empty slot
   }

   int hashFunction(int key) {
      return key % TABLE_SIZE;
   }

   void insert(int key) {
       int index = hashFunction(key);
       int i = 0;

       while (table[(index + i * i) % TABLE_SIZE] != -1 && i < TABLE_SIZE) {
          i++;
       }

       if (i < TABLE_SIZE) {
          table[(index + i * i) % TABLE_SIZE] = key;
       } else {
          cout << "Hash Table is full. Cannot insert " <<key <<endl;
       }
   }

   void display() {
      for (int i = 0; i < TABLE_SIZE; i++) {
         if (table[i] != -1)
            cout << i << " --> " << table[i] << endl;
         else
            cout << i << " --> Empty" << endl;
      }
   }

   ~HashTable() {
      delete[] table;
   }
};

int main() {
   HashTable ht;
   ht.insert(23);
   ht.insert(43);
   ht.insert(13);
   ht.insert(27);

   cout << "Hash Table with Quadratic Probing:\n";
   ht.display();

   return 0;
}

Following is the output of the above code ?

Hash Table with Quadratic Probing:
0 --> Empty
1 --> Empty
2 --> Empty
3 --> 23
4 --> 43
5 --> Empty
6 --> Empty
7 --> 13
8 --> 27
9 --> Empty
Updated on: 2025-05-28T16:51:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements