A summary of the binary search of the basic algorithm

Source: Internet
Author: User

This blog is I do the title and read a summary of various situations.

Reference to the book, the Deng Junhui teacher wrote the "data structure (c + + language version)" (3rd edition), but also the user's summary (the following will give the corresponding link).

First, find the position equal to the target element (if multiple, only need to find)

1/the body of the binary lookup2 intBinarySearch (intA[],intNinttarget)3 {4     intLo =0, HI = n; n the number of elements a of an array5     6      while(Lo <hi)7     {8         intMid = lo + (hi-lo)/2;9         if(A[mid] = =target)Ten             returnmid; One         Else if(Target <A[mid]) AHi =mid; -         Else -Lo = mid +1; the     } -     return-1; -}

Here are a few things to note about the right boundary hi:

1) in the hi=noutside the while loop, the description is the front open post-closed ([0,n) ), then the value of hi in the while loop should constitute a front-open closed ([Lo,mid )), at this time the condition of the while loop is Lo;

2) in the hi=n-1outside the while loop, the description is the front open post-closed type ([0,n-1]), then the value of hi in the while loop should constitute a pre-open closed type ([lo,mid-1]), At this time the condition of the while loop is Lo<=hi ;

This is not to say that the use of other situations can not be used, but sometimes error.

Second, look for the subscript of the first value of the repeating element, and return the first subscript that is not less than the target value if there is no duplicate element .

1 //the body of the binary lookup2 intBinarySearch (intA[],intNinttarget)3 {4     intLo =0, hi =N;5     6      while(Lo <hi)7     {8         intMid = lo + (hi-lo)/2;9         Ten         if(A[mid] <target) OneLo = mid +1; A         Else -Hi =mid; -     } the     returnHi; -}

Note : The HI value here can also change as one, to correspond to the change. Only after this change, in the absence of a target value, returns the first subscript that is less than the target value (from the target to the left).

third, to find the last subscript of a repeating element, or when there is no target value, the first subscript less than the target value. in this case, teacher Deng's book has strict proof.

1 //the body of the binary lookup2 intBinarySearch (intA[],intNinttarget)3 {4     intLo =0, hi =N;5     6      while(Lo <hi)7     {8         intMid = lo + (hi-lo)/2;9         Ten         if(A[mid] <=target) OneLo = mid +1; A         Else -Hi =mid; -     } the     return--lo;//must be reduced -}

Note: The difference between the second and the third is the difference between the IF condition judgment statement, and the return value,

Four, the two or three can be combined to find the existence of multiple target values, the target value of the interval. For more information, see Search for a range.

Version one of the evolutionary version of the Fibonacci lookup, which is the main difference between the binary search is the middle point mid is the use of Fibonacci selection, version three of the branches of the average check better, see reference books for details.

Summarize

For binary search, it is worth noting the selection of the interval (see the description in the first case); If the target value under a given condition (first or last) subscript, can be achieved by changing the equal sign within the IF condition in the while loop.

The following is a third case test program, the first to second can be achieved by changing the values contained in array A.

1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 intBinarySearch (intA[],intNinttarget);7 8 intMain ()9 {Ten     intA[] = {0,1,2,3,4,4,4,4,5,6,7,8,9 }; One     inttarget =4; A     intres = BinarySearch (A, -, Target);  -cout <<"The subscript for the target value is:"<< Res <<Endl; -     return 0; the } -  - //the body of the binary lookup - intBinarySearch (intA[],intNinttarget) + { -     intLo =0, hi =N; +      A      while(Lo <hi) at     { -         intMid = lo + (hi-lo)/2; -          -         if(A[mid] <=target) -Lo = mid +1; -         Else inHi =mid; -     } to     return--lo;//must be reduced + } -  the  * /* $ int a[] = {0,1,2,3,4,4,4,4,5,6,7,8,9},target=4, the return value is 7Panax Notoginseng int a[] = {0,1,2,4,4,4,4,5,6,7,8,9},target=3, the return value is 2 - int a[] = {0,1,2,4,4,4,4,5,6,7,8,9},target=3, return lo, return value 3 the */

If you crossing found what problems, a lot of comments Kazakhstan, common progress ~

Reference:

http://www.61mon.com/index.php/archives/187/

http://blog.csdn.net/sunmenggmail/article/details/7540970

Http://www.cnblogs.com/luoxn28/p/5767571.html

Http://www.cnblogs.com/grandyang/p/6854825.html

A summary of the binary search of the basic algorithm

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.