I made a mistake---two points to find

Source: Internet
Author: User

Two-point search is very difficult ah ... The journey is endless. Before reading the first time before the realization of the cycle, the termination of the condition is only to determine whether the mid-value falls within the range. Of course this is wrong, and it's rather stupid.

The second time after the implementation of the network to change the interpretation of the cycle of the condition into the while (L < h), the direct return-1; for my implementation, I made another mistake, this condition will be less compared to an element, that is, less than L = h point to the element.

Finally, I changed the loop condition to the while (L <= h) without a test error.

But maybe my test case might not be completely covered, and I'll continue with the argument tomorrow.

Two-point lookup also has an important error point is the overflow of integer addition ... Do not know how many of the world's bugs are out of the overflow, it must be more to stay in mind.

The solution given in the book is MID = L + ((h-l) >> 1);

is the mathematical formula (A + b)/2 = B + (b-a)/2 to be applied.

I made a mistake---two points to find

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.