Implementation of binary search in C language
Binary search, also known as half search (Binary Search), is a more efficient search method. However, half-and-half lookup requires that the linear table must have a sequential storage structure, and the elements in the table are arranged in order by keyword.
Usually when you look up an array, you will find that finding one by one is a waste of resources, so it is realized by binary lookup.
Algorithm requirements:
1. A sequential storage structure must be adopted.
two。 Must be arranged in order by keyword size.
First of all, assuming that the elements in the table are arranged in ascending order, compare the keywords recorded in the middle of the table with the search keywords, and if the two are equal, the search is successful; otherwise, the table is divided into the first and the last two sub-tables by using the intermediate location records. if the keyword of the intermediate location record is greater than the search keyword, then further look for the previous sub-table, otherwise further look for the latter sub-table. Repeat the above process until a record that meets the criteria is found, making the search successful, or until the child table does not exist, where the lookup is not successful.
(above is a mind map)
# include#include / / while, do while, goto, for, Recursive void digui (int shang,int xia,int zhong,int num) {zhong = (shang + xia) / 2; if (shang