Get the App
SLTechnology News&Howtos  ›  Network Security  › 

C language binary search (half search) algorithm and code

Shulou Source: shulou.com Published: 2022-06-01 04:01:22 10月02日 Update

Binary search is also known as half-and-half search, its advantage is that the search speed is fast, and the defect is that the data to be found by the request must be an ordered sequence. The basic idea of the algorithm is to compare the data of the two positions of the sequence you are looking for with the elements you are looking for. If they are equal, you will win. Otherwise, the sequence you are looking for will be divided into two parts based on that status. Then, according to the order discipline of the search sequence and the relationship between the two elements and the size of the element, we choose the local sequence where the element can exist, and take a different way to stop the search. Until you can be sure whether the element you are looking for can exist, the detailed usage can be understood in detail through the following code.

# include binarySearch (int a [], int n, int key) {int low = 0; int high = n-1; while (low

Tags: Element sequence victory both ends status algorithm local process code next information method data facet input order superscript subscript function function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei Shulou Technology OPPO Reno MySQL