Can you know, in fact, there is an O (1) in the search, which is called the second kill.
Hash Lookup:
Yes, he's a hash lookup, and when it comes to hashing, you're going to have to mention the hash function, oh, it's already in our heads.
Inherent in thinking. Be sure to know the corresponding relationship in the hash.
For example: "5" is a number to save, and then I throw to the hash function, the hash function to return me a "2", then the "5"
and "2" on the establishment of a corresponding relationship, this relationship is the so-called "Kazakhstan-Greece relationship", in the actual application also formed a "2" is key, "5" is value.
Then some friends will ask how to do hash, first do hash must adhere to the two-point principle:
①: Key as far as possible scattered, that is, I lost a "6" and "5" to you, you return a "2", then such a hash function is not perfect.
②: hash function as simple as possible, that is to say, throw a "6" to you, you hash function to do 1 hours to give me, so it is not good.
In fact, there are "five kinds" of commonly used hashing techniques:
The first one: "Direct addressing Method".
It's easy to understand, key=value+c; this "C" is a constant. Value+c is actually a simple hash function.
The second: "Division of the remainder method."
It's easy to understand, key=value%c, and explain ditto.
The third type: "Digital Analysis Method".
This is interesting, like a group of value1=112233,value2=112633,value3=119033,
For such a number we analyze the number of the middle two number of fluctuations, the other number unchanged. Then we can take the value of the key to be
Key1=22,key2=26,key3=90.
The fourth kind: "Square to take medium method". Ignore here, see the meaning of the name.
The fifth kind: "Folding method".
This is interesting, like value=135790, which requires a key to be a 2-digit hash value. So we turn value into 13+57+90=160,
Then remove the high "1", at this time key=60, Haha, this is their Kazakhstan-Greece relationship, so that the purpose of the key and each of the value is phase
Off, to achieve the "hash address" as far as possible scattered targets.
Is the so-called often walk along the river, where there is not wet shoes. Hash is also the same, you design the hash function again good, do not make any time hit the building, then throw us the question
is to resolve the "hash address" conflict.
In fact, the common methods of conflict resolution are 2 kinds:
First: "Open Address Law".
The so-called "open address" is actually an unused address in the array. In other words, in the place where the conflict occurs, the element that is later arrived (can be used in two ways
: ① linear probes, ② function probes to the array after looking for "open address" and then insert themselves into the.