📜  散列中的二次探测

📅  最后修改于: 2021-10-28 01:34:30             🧑  作者: Mango

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

  • 如果插槽 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

时间复杂度: O(N * L),其中 N 是数组的长度,L 是哈希表的大小。
辅助空间: O(1)。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程