Ec (2); Hash tables generally use comparison-based search methods. The search efficiency depends on the number of comparisons. In fact, if the ideal search is not compared, the query records can be obtained after one access, then, a definite correspondence relationship f must be established between the storage location of the record and Its keywords. In this way, when k is searched, as long as f finds the image f (k) of the given k according to the corresponding relationship ). F is called the hash function. A table created in this way is called a hash table (also called a hash table ). Hash Tables are easy to access but are prone to conflicts during storage (c script ec (2); script
Hash table
The general search method is based on comparison, and the search efficiency depends on the number of comparisons. In fact, the ideal search can be queried without comparison.
Record, you must establish a definite ing relationship between the storage location of the record and its keyword f. In this way, if you search for k
F finds the image f (k) of the given value k ). F is called the hash function. A table created in this way is called a hash table (also
Is called a hash list ). Easy to access but easy to store hash tables (collision): different keywords can correspond to the same hash address. For example
It is critical to determine the hash function and resolve conflicts.
1. Construction of Hash Functions
Direct addressing: H (k) = k or H (k) = a * k + B (linear function)
For example, population statistical table
1 2 3... 100
Age 1 2 3... 100
Number of students 67 3533 244... 4
Digital Analysis Method: A hash address composed of several digits of a keyword.
For example, the keyword is as follows: if the hash table length is 100, the two digits in the middle can be used as the hash address.
81346532 81372242 81387422 81301367 81322817 81338967 81354157
Square Method: After the keyword is square, the number of digits in the middle form the hash address.
Fold: split key numbers into several parts with the same number of digits (the digits in the last part can be different), and then overlay and
As the hash address.
Except for the remainder: the remainder obtained after the keyword is divided by a number p not greater than the table length m is the hash address.
H (k) = k mod p <= m
Random Number Method: H (k) = rondom (k ).
2. methods for handling conflicts
Assume that the address set is 0 .. n-1, and the location where the hash address obtained by the keyword is j (0 <= j <= N-1) already exists. The key is to process conflicts.
Find another empty hash address. An Address Sequence Hi I = 1, 2,... k may be obtained during processing.
0 <= Hi <= N-1), that is, if another hash address H1 is still in conflict when processing the conflict, find the next address H2. If the hash address H1 is still in conflict, then find
H3 .... How to get the Hi?
Open address method: Hi = (H (k) + di) mod m (H (k) is a hash function; m is the hash table length; di is an incremental sequence)
When di is 1, 2, 3, M-1, it is called linear detection and then hashed.
When di = 12,-12, 22,-22,32,-32,..., k2,-k2, it is called a secondary probe and then hash.
When di = random (m), it is called a pseudo-random detection sequence.
For example, the key words of the hash table with the length of 11 are 17,60, 29, respectively. The hash function is H (k) = k mod 11, and the key word of the fourth record is 38.
The address of the hash table is, 3 (random number = 9 ).
Hash method: Hi = RHi (key) I = 1, 2,..., k, where RHi is a different hash function.
Link address method: This method is like base sorting. The key values of the same addresses are all linked to the corresponding linked list.
Create a public welfare partition: set up another overflow table, regardless of the hash address, and fill in the overflow table in case of a conflict.
3. query a hash table
For example, the following set of keywords are processed by hash function H (k) = k mod 13 and linear probing for conflicting 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 k = 84 is given, first compare with a [6] in sequence with a [7], and a [8], then the result is a [8] = 84.
When k = 38 is given, it is first compared to a [12] and then to a [13]. Because a [13] does not exist, the search fails, there is no record in the table where the keyword is 38.
.
5.5 search for the k-th small element
Find the k-th small element, that is, find the k-th small element in n elements (unordered. The method is the same as the quick sorting method and uses recursion.
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 If I 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.