📜  散列中的二次探查

📅  最后修改于: 2021-05-07 09:23:06             🧑  作者: Mango

散列是对直接访问表的改进。想法是使用哈希函数,该函数将给定的电话号码或任何其他键转换为较小的数字,并使用该较小的数字作为称为哈希表的表中的索引。
Hash函数给定大的数字转换为实用小整型值的函数。映射的整数值用作哈希表中的索引。简而言之,哈希函数将一个大数字或字符串映射为一个小的整数,可用作哈希表中的索引。
在本文中,讨论了碰撞技术,二次探测
二次探测:二次探测是一种开放式寻址方案,如果给定的哈希值x碰撞哈希表,我们将在第i次迭代中寻找i 2个时隙
二次探测如何完成?
令hash(x)为使用hash函数计算的插槽索引。

  • 如果插槽hash(x)%S已满,则尝试(hash(x)+ 1 * 1)%S。
  • 如果(hash(x)+ 1 * 1)%S也已满,那么我们尝试(hash(x)+ 2 * 2)%S。
  • 如果(hash(x)+ 2 * 2)%S也已满,那么我们尝试(hash(x)+ 3 * 3)%S。
  • 对i的所有值重复此过程,直到找到空插槽。

例如:让我们考虑一个简单的哈希函数为“ key mod 7 ”,并将键序列为50,700,76,85,92,73,101

下面是上述方法的实现:

C++
// C++ implementation of
// the Quadratic Probing
#include 
using namespace std;
 
// Function to print an array
void printArray(int arr[], int n)
{
    // Iterating and printing the array
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
}
 
// Function to implement the
// quadratic probing
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, N);
}
 
// Driver code
int main()
{
    int arr[] = {50, 700, 76,
                 85, 92, 73, 101};
    int N = 7;
 
    // Size of the hash table
    int L = 7;
    int hash_table[7];
 
    // Initializing the hash table
    for (int i = 0; i < L; i++)
    {
        hash_table[i] = -1;
    }
 
    // Quadratic probing
    hashing(hash_table, L, arr, N);
    return 0;
}
 
// This code is contributed by gauravrajput1


Java
// 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);
    }
}


Python3
# Python3 implementation of
# the Quadratic Probing
 
# Function to pran array
def printArray(arr, n):
     
    # Iterating and printing the array
    for i in range(n):
        print(arr[i], end = " ")
     
# Function to implement the
# quadratic probing
def hashing(table, tsize, arr, N):
     
    # Iterating through the array
    for i in range(N):
         
        # Computing the hash value
        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 j in range(tsize):
                 
                # Computing the new hash value
                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, N)
 
# Driver code
arr = [ 50, 700, 76,
        85, 92, 73, 101 ]
N = 7
 
# Size of the hash table
L = 7
 
hash_table = [0] * 7
 
# Initializing the hash table
for i in range(L):
    hash_table[i] = -1
     
# Quadratic probing
hashing(hash_table, L, arr, N)
 
# This code is contributed by code_hunt


C#
// 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


Javascript


输出:
700 50 85 73 101 92 76