Two-point Search

Source: Internet
Author: User

Citation:Http://blog.csdn.net/int64ago/article/details/7425727int64Ago

See this title no matter what kind of psychology you are in, I think it's worth it. Because the problem is too simple, any start-to-touch "real" algorithms are basically starting with a two-point lookup. As for the two-point search do not know what is the first to find other information to see, and then to see this article. Since it's very simple, let's start by writing a bar that asks for the num[]={1,2,2,4,4,8,10} non-decrement sequence in the interval [0,7) to find, of course, we have to first guarantee to find the number E satisfies: num[0] <= e <= num[0], This is easy to do, in order to simplify and lose the representativeness, E selected 2, 3, 4 of these three numbers. Let's start writing together:

First, it's easy to write the int bsearch (int begin, int end, int e)

Then, it is natural to define an int mid, left = begin, and right = end;

How do you write it next? while (left < right)? while (left <= right)? while (mid = = left)? while (mid = = right)? .................. A real writer will want to get tangled up for a while, and we'll pick one. while (left < right)

Below, also very natural, Min = (left + right) >> 1; Using a bit cloud can save some time!

What now? It's time to tangle, isn't it? if (Num[mid] > E)? if (Num[mid] >= e)? We can pick one, if (Num[mid] > E)

In fact, the following you will continue to tangle ... right = mid; This is a normal person's writing, but sometimes you will see others write right = Mid-1; Let's think about it, but now we're going to write straight = mid;

If there is, of course there will be else, then take for granted left = mid;

Yes, the whole while loop is done, the end is to return, write down return when is not again tangled? Left Right Mid Forget it, write mid, the whole program is written, as follows:

[CPP]View Plaincopyprint?
  1. int bsearch (int begin, int end, int e)
  2. {
  3. int Mid, left = begin, right = end;
  4. While (left < right)
  5. {
  6. Mid = (left + right) >> 1;
  7. if (Num[mid] > E) right = mid;
  8. else left = mid;
  9. }
  10. return mid;
  11. }

Add the whole program and run it! Look for 2, 3, 4 when no results appear!! For example, when you check 4, one-step debugging will find that when mid=4,left=4,right=5, the next is in the while loop, remain unchanged! Where the hell is the problem? The problem in many places, because we encounter a lot of choices above, no result is a combination of multiple choices of results, through tinkering can also get the desired results, other examples do not give, directly talk about the key of this article discussion. I summed up the two points is nothing more than 4 cases: Yes_left, Yes_right, No_left, No_right, respectively: can find and return to the leftmost position of the number (such as when looking for 4 to return to position 3), The position that can be found and returns to the rightmost number (for example, when looking for 4 returns position 4), a position that cannot be found and returns to the left with a close number (for example, return position 2 when finding 3), a position that cannot be found and returns to the right of the nearest number (for example, return position 3 when finding 3). Here is the code I summarize debugging:

For Yes_left or No_right

  1. int bsearch (int begin, int end, int e)
  2. {
  3. int Mid, left = begin, right = end;
  4. While (left <= right)
  5. {
  6. Mid = (left + right) >> 1;
  7. if (Num[mid] >= e) right = mid-1;
  8. Else left = mid + 1;
  9. }
  10. return left;
  11. }

For Yes_right or No_left

  1. int bsearch (int begin, int end, int e)
  2. {
  3. int Mid, left = begin, right = end;
  4. While (left <= right)
  5. {
  6. Mid = (left + right) >> 1;
  7. if (Num[mid] > E) right = mid-1;
  8. Else left = mid + 1;
  9. }
  10. return right;
  11. }

Do not do too much, one-step debugging will naturally find the execution process, to illustrate that, two programs are used right = mid-1; left = mid +1; This is the premise of the termination condition if the left <= right. Note that some binary lookup is not only one of four cases, but a combination of use, such as finding a number, if found then xxx or XXX, if it is Yes_left | | No_right combination or Yes_right | | No_left combination directly with the above code can, otherwise it will be combined with, add some judgment and so on, because when not much, do not give the code, if you encounter can try to write, as a template, and then directly with ~

Now, isn't it easy to find two points? If you have summed up the words ...

Two-point 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.