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..