Getting Started with binary algorithms--two points search

Source: Internet
Author: User

binary Search, whether it is from the name or theory are very simple an algorithm, its broad and profound, simply scary.    Jon bentley:90% The programmer is unable to write out the binary lookup code correctly.

Others don't know, anyway I wrote in the morning for a long time, this search algorithm will find the complexity from O (n) down to O (Logn), well-deserved good algorithm, is one of many advanced algorithm optimization strategy.

  

  The basic thinking of binary search

Although binary search is a very hanging algorithm, but like many algorithms, need to use the basic conditions-sequence order!

First assume a monotone non-increment sequence: 1 2 3 4 5 6, find the location of 3, the Earth people will immediately think so-one to find, but this is very slow!

So someone wants to speed up the search, and they find that if they start looking in the middle, they can eliminate at least half the number after each comparison, which is the basic idea of binary search-binary.

1) Set three pointer variables (should be a pointer-nature variable, the index is also possible)--low, mid and High, and meet mid = (low + high)/2;

2) Set the loop, compare the target value with the mid storage value, update the low or high according to the comparison result;

3) in 2), the mid pointer is returned if the target is found, and a null pointer is returned if it has not been found.

Here's the code:

//Method one
intBinary_search (intNintv) { intlow=0, high=n-1, Mid; while(low<=High ) {Mid= (Low+high)/2; if(a[mid]==v)returnmid; if(a[mid]<v) Low=mid+1; if(a[mid]>v) high=mid-1; } return-1;}

  There seems to be no problem, in fact in some cases the answer would be strange: 1 2 2 4 5, if we look for 2, the answer, 2 (this is the index!). )

So the question comes, why the second one, not the first one? In fact, when the sequence appears repeating elements, we find the "any" one of course!

But in fact often we need to deal with sequences that always have duplicate elements, so we need to optimize!

Let's analyze the original binary to find--left < mid <right, that is, we give the "=" situation all to mid processing, so mid gives you all sorts of answers:

So, we might as well follow this rule for two minutes--left <= mid < Right!

We can look at the code:

//Method Two: Return to the first position
intLowerbound (Const intA[],Const intSizeConst inttarget) { intlow=0, high=size-1, Mid; while(low<High ) {Mid= (Low+high)/2; if(a[mid]<target) low=mid+1; ElseHigh=mid; } if(A[low]==target)returnLow ; Else return-1;}

  Here we will find two ways to find the most obvious difference: The first method is to find the answer to return immediately, the second method is to find the bottom (as a tree) and then return! Feeling method two to one

Straight to find the bottom seems to be very slow, but it turns out: method One is the slowest, so we can discard the first one!

Of course, the second method is to return to the first position, then there will be a return to the last position of the algorithm!

On the code:

//Method Three: Return to the last position
intUpperbound (Const intA[],Const intSizeConst inttarget) { intlow=0, high=size-1, Mid; while(low<High ) {Mid= (Low+high)/2+1; if(a[mid]>target) high=mid-1; Elselow=mid; } if(A[high]==target)returnHigh ; Else return-1;}

  The above is our binary search algorithm, reference: Data structure and programming--c++ language description (This is really a good book!) )

  

    

  

Getting Started with binary algorithms--two points 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.