The path to re-algorithm-Binary Search

Source: Internet
Author: User

The path to re-algorithm-Binary Search

 

 

 

Affiliated to -- recursion and sub-Governance

 

Description: given n elements a [0; n-1] In sorted order, we need to find a specific element x in these n elements.

 

Simple Method: of course, this is a good saying. The most stupid method is sequential search. Compare the elements from 0 to n-1 one by one until you can find element x or search all elements, it is determined that x is not in the element. This method also uses n elements to sort the order conditions. Therefore, in the worst case, the time complexity is O (n) times.

 

Binary Search: This method makes full use of the order relationship between elements and adopts the grouping policy. In the worst case, it can use O (log (n) Time to complete the search task. The basic idea is to divide n elements into two halves with the same number. Take a [n/2] for comparison with x,

If x = a [n/2], locate x and terminate the algorithm.

If x

If x> a [n/2], continue searching only in the right half of array a (n/2 to n-1 ).

 

Code Program:

 

Template <class Type> int BinarySearch (Type a [], const T & x, int n) {int l = 0, r = n-1; while (l <= r) {int m = (l + r)/2; if (x = a [m]) return m; if (x <a [m]) r = m-1; else l = m + 1;} return-1; // If NO x is found,-1} is returned}


 

Analysis: the code above shows that the size of the array to be searched is reduced by half every while loop of the algorithm executed.

Therefore, in the worst case, the while array executes O (log (n) times, while the code execution time in the loop body is about O (1 ).

In the end, the time complexity of the entire algorithm in the worst case is O (log (n )).

 

TIPS: the idea of a binary search algorithm is easy to understand, but it is not easy to write a correct binary search algorithm. Knuth mentioned in his book The Art of Computer Programming: Sorting and Searching that The first binary search algorithm appeared as early as 1946, however, the first fully correct binary search algorithm did not appear until 1962.

 

 

 

* *********************************** Please specify the source: bytes ********************************************

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.