[Algorithm-search 2] Binary Search

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.