Why is hash query faster?

Source: Internet
Author: User

Someone accidentally asked a question today, "why is hash query faster ?"

I didn't think much about it. I replied, "It's because the key-Value Pair method is used to quickly index the memory-worthy address."

Then I asked, "What is the principle of key-value pair? "

At that time, I did not know how to answer the question, so I had to say "sorry".

 

I have no books at hand, and I am too lazy to explain the terms. I found a sentence on the Internet:

When a key-value pair is inserted to hashtable, hashtable uses the gethashcode () method of the object referenced by each key to obtain a hash code and store it in hashtable.

In short,
(1) hash function rules are as follows: through a certain conversion relationship, the keywords are evenly distributed to the ordered structure of the specified size. The more scattered the query, the less time the query will take and the higher the space complexity.
(2) What have we paid for using hash?
Hash is a typical Algorithm for changing space time.For exampleAn array of 100 characters in length,To search for it, you only need to traverse and match the corresponding records. In terms of space complexity, if the array stores byte dataThis array occupies bytes of space. Now we useHash Algorithm, The hash we mentioned earlier must have a rule to constrain the relationship between the key and the storage location, so a fixed-length hash table is required. At this time,Still 100byte ArrayAssume that we need 200 bytes to record the relationship between keys and locations, the total space is bytes, and the table size used to record rules may vary according to the rules, for example, in the LZW algorithm, if a long byte array is used to record pixels and to record the tablespace of the relationship between locations and keys, the algorithm recommends an integer of 12 bits, the LZW algorithm uses variable-length encoding to distribute long enough pixel arrays to such tables.
Note: The most prominent problem in the hash table is the conflict, that is, the index locations calculated by the hash function for the two key values may be the same..

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.