Simple understanding of hash algorithm

The reason why the hash algorithm access is fast is that it directly obtains the memory storage location of the record to be accessed through the keyword key

Just imagine a scene like this, you really want to learn Tai Chi, and I heard that there is a guy named Zhang Sanfeng who is very good at school, so you

Go to the student office of the school to find someone, and the staff of the student office may come up with a list of students and search them one by one.

In the end, I will tell you that there is no such person in the school, and that Zhang Sanfeng has lived in Wudang Mountain hundreds of years ago. But if you
find the right person, such as looking for those students who love sports on the playground, they will tell you, "Oh, you are looking for Zhang Sanfeng,
there is something, I'll take you there. So he took you to the gym, And tell you, the little boy who taught everyone to do Tai Chi

The guy is Zhang Sanfeng', it turns out that "Zhang Sanfeng is nicknamed because he is good at Taijiquan. The teacher

Looking for Zhang Sanfeng, that is, a sequence table search, which relies on the comparison of name keywords. And through students who love sports

When asking, there was no traversal or comparison, just based on their experience of "if you want to find Tai Chi 'Zhang Sanfeng', you must be in the gym",

tell you the location directly.

That is to say, we only need to pass a certain function f, so that the

storage location = f (keyword)

, so that we can obtain the storage location of the required record by searching for the key without comparing. This
is a new storage technology—hash technology (hash algorithm).

Hash technology is to establish a definite correspondence f between the storage location of the record and its key, so that
each key key corresponds to a storage location f (key)
.
When searching, find the mapping f (key) of the given value
key according to the determined correspondence . If this record exists in the search set, it must be at the position of f (key).
Here we call this correspondence f a hash function, also known as a Hash function . According to this
idea , the records are stored in a continuous storage space using hashing technology, and this continuous storage space is called
a
hash table or a Hash table . Then the record storage location corresponding to the keyword is called the hash address.

The whole hashing process is actually two steps:
(1) When storing, calculate the hash address of the record through a hash function, and store the record according to this hash address.
(2) When searching for a record, we calculate the hash address of the record through the same hash function, and access the record according to this hash
address . Since the same hash function is used for access, the result is of course the same.

Therefore, hashing technology is both a storage method and a search method. However, it is different from linear tables,
trees, graphs and other structures in that there is a certain logical relationship between the data elements in the previous structures, which can be used with
The connection diagram is shown, and there is no logical relationship between the records of the hash technology, it is only
associated . Therefore, hashes are primarily lookup-oriented storage structures.

We often encounter two keywords key1 != key2, but there is f(key1) = f(key2). This phenomenon is
called hash conflict. If there is no hash conflict, the hash table is a very efficient The lookup data structure of ,
its time complexity is O(1);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325910333&siteId=291194637