[Java data structure] how to avoid conflicts in the hash table


Preface

1. What is a hash table?

This is the answer given on Baidu:
Insert picture description here
Insert picture description here
In short, why is there such a data structure?
Because we want to get the elements we want to search from the table at a time without any comparison. Therefore, a hash table is constructed, and a one-to-one mapping relationship can be established between the storage location of the element and its key code through a certain function (hash function), so that we can find it out more quickly when searching. The element we want to find.
In the following code demonstration, a hash table is used at the bottom layer, so that each letter has a one-to-one correspondence with the number of times it appears in the string;

public static void main(String[] args) {
    
    
        String s="huddiolabcsjddddop";
        int[] count=new int[26];
        for(char ch:s.toCharArray()){
    
    
            int idx=ch-'a';
            count[idx]++;
        }
        System.out.println(Arrays.toString(count));
        

Second, what is a hash collision

1. Why do hash collisions occur

For the keywords Ki and Kj(i!=j) of the two data elements , but there are: Hash(Ki)==Hash(Kj) , that is: different keywords calculate the same hash address through the hash function , This phenomenon is called hash collision or hash collision.

2. Can hash conflicts be avoided?

First of all, we need to be clear, because the capacity of the underlying array of our hash table is often less than the number of keywords to actually store, (that is, when the range of the elements we often store is uncertain, there may be multiple elements The hash addresses are the same), which causes hash collisions to be inevitable, so what we can do is to reduce the collision rate as much as possible .

Three, how to resolve hash conflicts

1. Linear detection

For example, given an array, int[] array={4,5,6,9,1,7,44}
its insertion operation: 1. Get the position of the element to be inserted in the hash table through the hash function
2. If If there is no element at this position, insert it directly. If there is an element at this position, use the linear detection method to find the next empty position for insertion.
Insert picture description here

2. Zipper method

Insert picture description here
The zipper method can be considered to transform a search problem in a large collection into a search problem in a small collection.


to sum up

The efficiency of the linear detection method:
Insert picture description here

1. For all the elements in the hash table, what is the average number of comparisons?
(1+1+1+1+1+5+1)/7=1.57

2. For all elements that are not in the hash table, what is the average number of comparisons?
(For each subscript to judge whether it is continuous)
(1+2+1+1+7+6+5+4+3+2)/10=3.2.

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/109632573