Quadratic Probing in Hashing

Hashing is an improvement over Direct Access Table. The idea is to use a hash function that converts a given phone number or any other key to a smaller number and uses the small number as the index in a table called a hash table.

Hash Function: A function that converts a given big number to a small practical integer value. The mapped integer value is used as an index in the hash table. In simple terms, a hash function maps a big number or string to a small integer that can be used as an index in the hash table.

In this article, the collision technique, quadratic probing is discussed.

Quadratic Probing: Quadratic probing is an open-addressing scheme where we look for i2‘th slot in i’th iteration if the given hash vale x collides in the hash table.

How Quadratic Probing is done?
Let hash(x) be the slot index computed using the hash function.



  • If the slot hash(x) % S is full, then we try (hash(x) + 1*1) % S.
  • If (hash(x) + 1*1) % S is also full, then we try (hash(x) + 2*2) % S.
  • If (hash(x) + 2*2) % S is also full, then we try (hash(x) + 3*3) % S.
  • This process is repeated for all the values of i until an empty slot is found.

For example: Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76, 85, 92, 73, 101.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the Quadratic Probing
  
class GFG {
  
    // Function to print an array
    static void printArray(int arr[])
    {
  
        // Iterating and printing the array
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
  
    // Function to implement the
    // quadratic probing
    static void hashing(int table[], int tsize,
                        int arr[], int N)
    {
  
        // Iterating through the array
        for (int i = 0; i < N; i++) {
  
            // Computing the hash value
            int hv = arr[i] % tsize;
  
            // Insert in the table if there
            // is no collision
            if (table[hv] == -1)
                table[hv] = arr[i];
            else {
  
                // If there is a collision
                // iterating through all
                // possible quadratic values
                for (int j = 0; j < tsize; j++) {
  
                    // Computing the new hash value
                    int t = (hv + j * j) % tsize;
                    if (table[t] == -1) {
  
                        // Break the loop after
                        // inserting the value
                        // in the table
                        table[t] = arr[i];
                        break;
                    }
                }
            }
        }
  
        printArray(table);
    }
  
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 50, 700, 76, 85,
                      92, 73, 101 };
        int N = 7;
  
        // Size of the hash table
        int L = 7;
        int hash_table[] = new int[L];
  
        // Initializing the hash table
        for (int i = 0; i < L; i++) {
            hash_table[i] = -1;
        }
  
        // Quadratic probing
        hashing(hash_table, L, arr, N);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the Quadratic Probing
using System;
  
class GFG{
  
// Function to print an array
static void printArray(int []arr)
{
  
    // Iterating and printing the array
    for(int i = 0; i < arr.Length; i++)
    {
       Console.Write(arr[i] + " ");
    }
}
  
// Function to implement the
// quadratic probing
static void hashing(int []table, int tsize,
                    int []arr, int N)
{
  
    // Iterating through the array
    for(int i = 0; i < N; i++) 
    {
         
       // Computing the hash value
       int hv = arr[i] % tsize;
         
       // Insert in the table if there
       // is no collision
       if (table[hv] == -1)
           table[hv] = arr[i];
       else
       {
             
           // If there is a collision
           // iterating through all
           // possible quadratic values
           for(int j = 0; j < tsize; j++)
           {
                 
              // Computing the new hash value
              int t = (hv + j * j) % tsize;
              if (table[t] == -1) 
              {
                    
                  // Break the loop after
                  // inserting the value
                  // in the table
                  table[t] = arr[i];
                  break;
              }
           }
       }
    }
    printArray(table);
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 50, 700, 76, 85,
                  92, 73, 101 };
    int N = 7;
  
    // Size of the hash table
    int L = 7;
    int []hash_table = new int[L];
  
    // Initializing the hash table
    for(int i = 0; i < L; i++)
    {
       hash_table[i] = -1;
    }
      
    // Quadratic probing
    hashing(hash_table, L, arr, N);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

700 50 85 73 101 92 76

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.