Common HASH functions

The hash function can make the access process of a data sequence more rapid and effective. Through the hash function, the data element will be located faster. Commonly used Hash functions are:

1. Direct addressing method. Take the keyword or a certain linear function value of the keyword as the hash address. That is, H(key)=key or H(key) = a·key + b, where a and b are constants (this kind of hash function is called its own function)

2. Digital analysis method. Analyze a set of data, such as the date of birth of a group of employees. At this time, we find that the first few digits of the date of birth are roughly the same. In this case, the probability of conflict will be very high, but we find that the date of birth The last few digits that indicate the month and the specific date are very different. If the following digits are used to form a hash address, the probability of conflict will be significantly reduced. Therefore, the digital analysis method is to find out the laws of numbers, and use these data as much as possible to construct a hash address with a lower probability of conflict.

3. The square is the Chinese method. Take the middle bits of the squared key as the hash address.

4. Folding method. Divide the keyword into several parts with the same digits, and the last part can have different digits, and then take the superposition and (remove the carry) of these parts as the hash address.

5. Random number method. Choose a random function, and take the keyword as the seed of the random function to generate a random value as the hash address, which is usually used when the length of the keyword is different.

6. In addition to the remainder method. The remainder obtained by dividing the key by a number p not greater than the length m of the hash table is the hash address. That is, H(key) = key MOD p,p<=m. Not only can the keyword be directly modulated, but also after the folding and squaring operations. The choice of p is very important, usually a prime number or m. If p is not well chosen, collisions are likely to occur.

Guess you like

Origin blog.csdn.net/weixin_44371237/article/details/114694068