Algorithm-Binary Search
Sequential search [algorithm-search] is the simplest Search policy, which is easy to analyze and suitable for small-scale data. If the data size is large, the sequential search performance is unsatisfactory. In this case, you need to find a more efficient algorithm-binary search.Binary Search, also known as semi-query, provides excellent search performance, but the search data must be an ordered sequence.1. Sequential search:Core: first determine the scope of the target to be queried, and then gradually narrow down the scope until the search is successful or the search fails.The keyword key is compared with an Array [I] element in the table. There are 3 results:1. key = Array [I]. The search is successful.2. key> Array [I]. The possible range of elements to be queried is before Array [I.3.Key <Array [I]. The possible range of elements to be queried is after Array [I ]..Binary Search is based on the above principle: Compare the number of possible ranges with the key each time. If the values are equal, the search is successful. If the values are not the same, the search result is reduced. For example:Comparison between keywords and elements in the middle of the current range:Keyword> intermediate position element, then narrow down the range: Right side of the intermediate elementBy constantly narrowing the search scope, you can find that the search is successful or the search fails to be returned.
The example program is as follows:2. Time Complexity:The time complexity of binary search is log2 (N ).3. Binary Search Evaluation:Binary Search is more efficient, but requires sequential search. Sequence sorting is a high-cost operation. It is difficult to insert or delete data into or from a sequence. Therefore, binary lookup is especially suitable for tables with few changes but needs to be searched frequently.