The path of regaining algorithm--binary search

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************



Subordinate to--recursion and divide and conquer


Description : Given the ordered n elements a[0;n-1], now to find a specific element x in these n elements.


Naive Method : Of course, this is a good word, understand the most stupid method, that is, sequential search, compare 0 to n-1 elements, until the element x or search all the elements, to determine the X is not in the element, this method is well used n elements in the order of the conditions, so the worst case, Time complexity is O (n) comparison.


Two-point search : This method takes full advantage of the order relationship between elements and uses the divide-and-conquer strategy to complete the search task in the worst case with O (log (n)) time. The basic idea is to divide n elements into the same number of halves, taking A[N/2] and X as a comparison,

If X=A[N/2], then X is found and the algorithm terminates.

If X<A[N/2], the search continues only in the left half of array a (0 to N/2)

If X>A[N/2], the search continues 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;    X is not found, returns-1}


Analysis : As can be seen from the above code, the size of the array to be searched is halved by the while loop of the algorithm executed once.

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

Ultimately, the overall algorithm has an O (log (n) time complexity in the worst case scenario.


Small Knowledge : The idea of binary search algorithm is easy to understand, but it is not a simple matter to write a correct binary search algorithm. Knuth, in his book The Art of computer programming:sorting and searching, mentions that the first binary search algorithm appeared as early as 1946, But the first completely correct binary search algorithm didn't appear until 1962.




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

The path of regaining algorithm--binary search

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.