If the static query table is an ordered table, you can use semi-query.
The semi-Query Process is: first determine the range (range) of the record to be queried, and then gradually narrow down the range until the record is found or cannot be found. The half-fold search process compares the keywords recorded in the center of the interval with the given value. If the values are equal, the search is successful. If the values are different, the search is narrow, until the keyword recorded in the intermediate position of the new interval is equal to the given value or the size of the search interval is smaller than zero (indicating that the search is unsuccessful.
The keyword key is compared with an array [I] element in the table. There are three situations:
1. Key = array [I], search successful
2. Key> array [I]. The possible range of the elements to be searched is before array [I ].
3. Key <array [I]. The possible range of the element to be searched is after array [I ].
Take the following ordered table as an example to analyze the half-lookup method.
The ordered table is as follows)
<1> Search for keywords in an ordered table, for example, search for 21. The search process is as follows:
(05, 13, 19,21, 92)
↑
Low = 0 mid = (low + high)/2 = 5 high = 10
① Because array [Mid] = 56> 21, high = mid-1 indicates that if the element to be queried exists, it must be within the range [low, mid-1.
(05, 13, 19,21, 92)
↑
Low mid high = 5
② Because array [Mid] = 19 <21, low = Mid + 1 indicates that if the element to be queried exists, it must be within the range of [Mid + 1, high.
(05, 13, 19,21, 92)
Zookeeper
Low/Mid high = 5
③ Because array [Mid] = 21 indicates that the element to be queried exists.
<2> Search for keywords that do not exist in an ordered table, for example, 85. The search process is as follows:
(05, 13, 19,21, 92)
↑
Low = 0 mid = (low + high)/2 = 5 high = 10
① Because array [Mid] = 56 <85, low = Mid + 1 indicates that if the element to be queried exists, it must be within the range of [Mid + 1, high.
(05, 13, 19,21, 92)
Bytes
Low mid high
② Because array [Mid] = 80 <85, low = Mid + 1 indicates that if the element to be queried exists, it must be within the range of [Mid + 1, high.
(05, 13, 19,21, 92)
Bytes
Low/Mid high
③ Because array [Mid] = 88> 85, high = mid-1 indicates that if the element to be queried exists, it must be within the range [low, mid-1.
The search fails because the high value is less than low.
The binary search C language of an ordered table is implemented as follows:
/*************************************** * ***************************** Author: li Bing Date: 2014-9-24email: [email protected] ********************************** * ********************************/typedef int elemtype; # define eq (A, B) (a) = (B) # define LT (A, B) (a) <(B )) # define SCSI (A, B) (a) <= (B) int search_bin (elemtype array [], int num, int length) {int index_low, index_mid, index_high; index_low = 1; index_high = length; while (index_low <= index_high) {index_mid = (index_low + index_high)/2; If (eq (Num, array [index_mid]) return index_mid + 1; else if (LT (Num, array [index_mid]) index_high = index_mid-1; else index_low = index_mid + 1;} return-1 ;}
Applicability:It is highly efficient to search for large ordered tables. Suitable for tables that are rarely changed but frequently searched.
Advantages:
1. The half-fold search is more efficient than sequential search.
2. the time complexity of semi-query is log2 (n)
3. The average length of a half-fold query is log2 (n + 1)-1.
Disadvantages:
1. Semi-query is only applicable to ordered tables.
2. The half-fold query is limited to the sequential storage structure, and the linear linked list cannot be effectively searched.
Search Algorithm for algorithm learning: static search table (2) sequential table search