Hash table (hash table) lookup

Source: Internet
Author: User

1. What is hashing technology?

The hashing technique establishes a definite correspondencebetween the stored location of the record and the key word of the record , so that each keyword key corresponds to a storage location F (key).

When looking, the mapping F (key) of the given value is found based on this determined correspondence, and must be at f (key) if the record exists in the lookup collection.

Hashing technology is both a storage method and a lookup method.

2. What is a hash table?

A hash technique is used to store records in a contiguous storage space called a hash table (hash table).

The hash table is based on data and provides quick insert and delete operations.

3. Six ways to construct a hash function:

1, direct addressing method:

function formula: F (key) =a*key+b (A, B is constant)

The advantage of this approach is that it is simple, uniform, and does not create conflicts. However, it is necessary to know the distribution of the keywords in advance, which is suitable for finding small and continuous tables.

2, Digital analysis method:

For example, our 11-digit mobile phone number "136xxxx7887", of which the first three is the access number, generally corresponds to the sub-brand of different operating companies, such as 130 is Unicom Ruyi, 136 is mobile Shenzhou line, 153 is telecommunications. The middle four are the HLR identification number, which indicates the user's attribution. The last four are the real user numbers.

If we want to store a company employee registration form Now, if we use the mobile phone number as the keyword, then most likely the first 7 bits are the same, so we choose the following four as the hash address is a good choice.

3, square take the middle method:

So the name of meaning, such as the keyword is 1234, then its square is 1522756, and then extract the middle of the 3 is 227 as the hash address.

4, Folding Method:

The folding method is to divide the keyword from left to right into several parts equal to the number of digits (the last part is not short enough), and then add the portions to the hash table, and take the following several as hash addresses.

For example, our keyword is 9876543210, the hash table is three bits long, we divide it into four groups, 987|654|321|0, and then add them to sum 987+654+321+0=1962, and then ask for 3 bits to get the hash address of 962, haha, is not very interesting.

5, in addition to the remainder method:

function formula: F (key) =key mod p (p<=m) m is the hash table length.

This method is the most common method of constructing a hash function.

6, Random number method:

function formula: F (key) = random (key).

Here the random function, when the length of the keyword is not equal, the use of this method is more appropriate.

Two methods of resolving hash function conflicts:

Our best-designed hashing function is also unlikely to completely avoid collisions, and when we use a hash function we find two keyword Key1!=key2, but there is an F (key1) =f (Key2), which is a conflict.

Method One: Open addressing method:

The open addressing method is to look for the next empty hash address once a conflict occurs, as long as the hash table is large enough, the empty hash address is always found, and the record is inserted. This approach is the most common approach to conflict resolution.

Method Two: Chain address method:

3. Hashiha (1) Converts a word to an index directly as an index (2) * * Converts the letter to an ASCII code and then adds the multiplication of the power of the * * * Compression Optional value

Hash table (hash table) lookup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.