binary Find
Also known as binary search. This lookup method requires that the data for the lookup table be stored in a linear structure, and that the data in the lookup table is ordered from small to large in a keyword.
Binary lookup (binary lookup) is a simple and efficient search algorithm with a lookup length of up to ㏒2n+1 (the depth of the decision tree), an average lookup length of ㏒2 (n+1)-1, and a higher efficiency than the sequential lookup, but binary lookup can only be applied to sequentially stored ordered tables (for example, a linear list cannot be effectively binary-searched).
Implementation of classical non-recursive algorithm
int binary_search (int search_table[], int key, int low, int. high) {and low <= high} {Mid = (low + high)/2;if (Sear Ch_table[mid] < key) {low = mid + 1;} else if (Search_table[mid] > key) {high = mid-1;} Else{return Mid;}} whilereturn-1;}
Implementation of classical recursive algorithm
int binary_search (int search_table[], int key, int low, int. high) {if (Low > High) {return-1;} int mid = (low + high)/2;if (search_table[mid] = = key) {return mid;} else if (Search_table[mid] < key) {Binary_search (search_table, Key, Mid + 1, high);} Else{binary_search (search_table, Key, Low, mid-1);}}
However, as the precondition for binary lookup is that ordered table sequential storage is required, for static lookup tables, once the sorting is no longer changed, such an algorithm is better. However, for datasets that frequently perform insert or delete operations, maintaining an ordered ordering can have a small amount of work, which is not recommended.
Base algorithm-find: Binary find