Binary find out how to "fancy" sell-Moe

Source: Internet
Author: User

Binary search, dare say the goods buried in the soil into ash should be recognized. The principle of the idea is so simple and clear, the code is not bothered to go through reflex arc. But in fact, according to Knuth god Ben described, the first wooden bug in the binary search is the algorithm published 12 after the appearance, but later found that there are some small array of cross-border problems. And now, most of us are straight to the point of learning by the predecessors of the optimization of the algorithm, which is also called "T-Dayton" on the shoulders of giants, but also so.

Look back at the two points to find out how to "fancy" selling moe.

  problem: Given an ordered array a, and the elements in the array are duplicated, each of the target values targets the target value in the range of the subscript of the array (the interval between the starting subscript of the target element and the end subscript).

eg

int a[9]={0, 1, 2, 3, 3, 3, 4, 5, 6}, given target=3, the return interval is [3 5].

Analysis: Obviously this will inevitably use the idea of binary search, but to modify it slightly. As long as you find the left and right boundary subscript respectively.

  Left boundary

Version One:

  

Version two:

  

In fact, the two-point search for the left side of the closed eyes will not have a bug, but the right interval is a bit funny.

  Right border

Version One:

  

Well, this version is no problem, really happy!!

Wait, this version of the following is a bit capricious!

Version two:

  

Run it, dead loop! It should be said that in some cases the cycle will die.

Where is the problem?

Well, that's the low=mid problem.

Why is it?

Because when low+1 equals high and a[mid] equals target, mid equals low+ (high-low)/2 equals low, but mid is also assigned low, so the result is low and high are not moving, "in situ", the cycle ...

So, this version of the shrimp!

For example: a1[9]={1,2,3,3,3,5,7,8,9}; Running results will kill the ball!

a2[8]={1,3,3,3,5,7,8,9}; Run results will be ok!

In addition, the idea of using dichotomy to find the right boundary of interval is reflected in another small algorithm. ---------binary Insert Sort.

The binary insertion sort can reduce the time to find the direct insertion sort, but rather than the direct insertion sort, it only reduces the time to find the insertion position, while ensuring that the sort results are stable. But its ideas can be used for reference to drop!

by Qcer

2015.10.2

  

  

  

Binary find out how to "fancy" sell-Moe

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.