Introduction to Algorithms exercise 2.3-52 Search Binary Searches

Source: Internet
Author: User

Earth people know "binary search", the method is very simple, but can you write a bug-free program within 10 minutes?

Easier, write it yourself and try it.

 Public classBinarySearch { Public Static intSearchint[] A,intTargetintAintb) {intMiddle = (a+b)/2; if(a>b)return-1; Else if(a[middle]==target)returnMiddle; Else if(a[middle]<target)returnSearch (a,target,middle+1, B); Else             returnSearch (a,target,a,middle-1); }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intA [] = {2,4,6,8,10}; System.out.println (Binarysearch.search (A,1, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,2, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,3, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,4, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,5, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,6, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,7, 0, A.length-1)  ); System.out.println (Binarysearch.search (A,8, 0, A.length-1)  ); System.out.println (Binarysearch.search (A,9, 0, A.length-1)  ); System.out.println (Binarysearch.search (A,Ten, 0, a.length-1) ); System.out.println (Binarysearch.search (A,One, 0, a.length-1) ); }}

The above code seems to have no problem, but there is still a very subtle bug, this bug occurred in:

int  middle = (a+b)/2;

This line. Think about it, if the a+b exceeds the maximum value of the int type???
So, fixing the bug should look like this:
 Public classBinarySearch { Public Static intSearchint[] A,intTargetintAintb) {intMiddle = a + (b-a)/2; if(a>b)return-1; Else if(a[middle]==target)returnMiddle; Else if(a[middle]<target)returnSearch (a,target,middle+1, B); Else             returnSearch (a,target,a,middle-1); }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intA [] = {2,4,6,8,10}; System.out.println (Binarysearch.search (A,1, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,2, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,3, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,4, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,5, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,6, 0, a.length-1)  ); System.out.println (Binarysearch.search (A,7, 0, A.length-1)  ); System.out.println (Binarysearch.search (A,8, 0, A.length-1)  ); System.out.println (Binarysearch.search (A,9, 0, A.length-1)  ); System.out.println (Binarysearch.search (A,Ten, 0, a.length-1) ); System.out.println (Binarysearch.search (A,One, 0, a.length-1) ); }}

The end ...

Introduction to Algorithms exercise 2.3-52 Search Binary Searches

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.