Algorithm implementation review two points search

Source: Internet
Author: User

Previous words:

Why write this series?
The essence of the algorithm is not only a design of the algorithm, but also a good design of the algorithm. The former may require a good algorithmic engineer, while the latter requires an excellent programmer.
Many times we just want to know a design idea, but for programmers, a good implementation is very important.
The details of the implementation will determine success or failure. After all, programmers face and output programs, not ideas.

Reference:

Do you really find two points? http://blog.csdn.net/int64ago/article/details/7425727
Two points to find, you really will? Http://www.ahathinking.com/archives/179.html
Do you really write two-point search? Http://blog.chinaunix.net/uid-1844931-id-3337784.html

Body

To say the realization, must start from the idea, a starting position start and an end position ends.
Good
The problem comes, we know that the end of the loop is the start to more than end, then the equals sign?
The number of testcase1:1 we allow is not equal to the loop, for example:
while (Start<end) {}
A number of words directly do not check. The ok,= must be.
Okay, go on, what are we going to do when start<=end? The dichotomy, of course, is mid-fetch.
Mid= (Start+end)/2 or >>1 in fact the compiler will optimize, you think people silly? The end of the family will be recursive.
Then we compare the values of the mid position and the required values, then replace the start or end, and the problem comes again,
Question two: How to replace, or which value to replace?
We think, if the middle number is large, the value should be in the first half, end must be changed, OK, change to mid?
Here is a mistake, if the start and end intervals, mid is start, replacing with start may cause a dead loop. Over
So when the end is replaced with the Mid-1,start replaced by the time mid+1
Question three, does the equals sign need to be handled?
It is normal logic to first determine if it is equal to the middle value, and the equal sign returns directly.
And the first link above, inside is not =.
We think so, if it happens to be equal, but we do not return directly, and continue to look until start=end= (pos-1 or pos+1).
Press link 1 in the Yes_left method: the original array 1 2 5, find 2.
S=0 e=2 m=1 *mid>=val e=mid-1
S=0 e=0 mid=0 *mid<val s=mid+1 return S=1
Find 3 s=0 e=2 m=1 *mid<val s=mid+1
s=2 e=2 mid=2 *mid>=val e=mid-1 return s=2
The feature of this method is that the mid and Val comparisons are >= and return is the start
The description returns a location that is not less than the minimum value of Val
Yes_right Find 2.s=0 e=2 m=1 *mid<=val s=mid+1
s=2 e=2 mid=2 *mid>val e=mid-1 return e=1
If you are looking for 3 s=0 e=2 m=1 *mid<=val s=mid+1
s=2 e=2 mid=2 *mid>val e=mid-1 return e=1
This method is characterized by the mid and Val comparisons, which are returned by the end
Indicates that the position returned is not greater than the maximum value of Val
Jumping out of the inevitable s>e accurately said S=e+1, when the Val value is not found, S is larger than Val, E is smaller than the Val
Note that the bottom of this may return-1
Most of the time we have to find the number that is not more than the Val number, which is what we choose below +1
Question four, to further deal with some special cases.
Special circumstances include, the search is exactly the first. This is a case of judging before the cycle.
The addition of an equal sign within the loop can also speed up the search and exit.
OK, now look at the normal implementation

int bsearch (int data[],int len,int val) {   if (*data==val) return 0;   if (*data+len-1==val) return len-1;   int s=0,e=len-1,mid=0;   while (s<=e)   {      mid= (s+e)/2;      if (*data+mid==val) return mid;      if (*data+mid>val) e=mid-1;      else s=mid+1;   }    return e;      }

Algorithm implementation review 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.