What is hashing hash algorithm

Source: Internet
Author: User
Tags hash

Hash (hash) table

The general search method is based on comparison, lookup efficiency depends on the number of times, in fact, the ideal search hope without comparison, one access can be found

Record, you must establish a definite correspondence between the storage location of the record and the key word F, so that when you look for K, just under this

The corresponding relation F finds the image F (k) of the given value K. This corresponding relationship f is called hash function. The table that is created by this idea is called a Hashtable (also

Called a hash table). Hash tables are easy to access but store in conflict (collision): That is, different keywords can correspond to the same hash address. Such as

What determines the hash function and resolves the conflict is critical.

1. Method of constructing hash function

Direct Addressing method: H (k) =k or H (k) =a*k+b (linear function)

Such as: demographic statistics tables

Address 1 2 3 ... 100
Age 1 2 3 ... 100
Number 67 3533 244 ... 4

Digital Analysis Method: To take a number of keywords to form a hash address

For example: The key words are as follows: If the hash table length is 100, it is advisable to have the median two digits 10 as the hash address.

81346532 81372242 81387422 81301367 81322817 81338967 81354157 81368537

Square method: The keyword squared after the number of median to form a hash address

Folding method: The key number is divided into the same number of bits (the last part of the number of digits can be different) and then take a few points of the superposition and (shed

To carry) as a hash address.

Except the remainder method: The remainder is a hash address when the keyword is removed by a number p that is not greater than the table length m.

H (k) =k mod p p<=m

Random number method: H (k) =rondom (k).

2. Methods of dealing with conflicts

Suppose the address set is 0. N-1, the location of the hash address for J (0<=j<=n-1) that is obtained by the keyword already exists, and handling the conflict is the key

The record of the word finds another "empty" hash address. You may get an address sequence in processing Hi I=1,2,...K

0&LT;=HI&LT;=N-1), that is, in the process of dealing with a conflict if you get another hash address H1 still conflict, and then the next address H2, if still conflict, and then ask

H3 ..... How do you get hi?

Open addressable Method: Hi= (H (k) +di) mod m (h (k) is a hash function; M is a hash table long; Di is an increment sequence)

When di=1,2,3, ... m-1 is called linear probing and hashing.

When DI=12,-12,22,-22,32,-32,...,K2,-K2 is called two times to detect and then hash.

When Di=random (m) is called pseudo random detection sequence.

Example: the hash table keyword of length 11 is 17,60,29, the hash function is H (k) =k MoD 11, and the fourth key word is 38

, the address of the hash table added by the above method is 8,4,3 (random number = 9).

Again Hashifa: Hi=rhi (key) i=1,2,..., K, where RHI are all different hash functions.

Chain Address method: This method is very much like the Cardinal order, the same address keyword values are linked to the corresponding list.

Establishment of the Public welfare Zone law: another overflow table, regardless of the resulting hash address, once the conflict, are filled in the overflow table.

3. Hash Table Search

Example: The next set of keywords by hash function h (k) =k mod 13 and linear detection handles conflict-derived hash table a[0..15]:

0 1 2 3 4 5 6 7 8 9 10 11 12 13-14 15
14 01 68 27 55 19 20 84 79 23 11-10

When the given value is k=84, the first and a[6] are found to be more successful than the result a[8]=84 in sequence and a[7],a[8].

When the given value k=38, then first and a[12] than, and a[13] than, because a[13] No, the lookup is not successful, the table does not exist the keyword equals 38

Recorded.

5.5 Find K small elements

Find the element k small in n elements (unordered) by finding the small k element. Method with fast sorting, using recursive method.

The procedure is as follows:

Program KSPV;
Const N=7;
Type
ARR=ARRAY[1..N] of Integer;
Var
B:arr;
I,k:integer;
function P (s,t:integer): integer;
var I,j,t1,x:integer;
Begin
I:=s;j:=t;x:=b[i];
Repeat
while (b[j]>=x) and (j>i) do j:=j-1;
If J>i then begin t1:=b[i]; B[i]:=b[j];b[j]:=t1;end;
while (b[i]<=x) and (i<j) do i:=i+1;
If I<j then begin T1:=B[J];B[J]:=B[I];B[I]:=T1; End
Until I=j;
B[i]:=x;
P:=i;
End
function Find (s,t,k:integer): integer;
var P1,q:integer;
Begin
If S=t then Find:=b[s] Else
Begin
P1:=p (s,t);
q:=p1-s+1;
If K<=q then Find:=find (s,p1,k) Else Find:=find (P1+1,T,K-Q);
End
End
Begin
Write (' Input data: ');
For I:=1 to n do read (b[i]); readln;
Write (' Input k: '); Read (k);
Write (' Output data: ');
Writeln (' kthsmall:= ', find (1,n,k));
End.

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.