A summary of the binary algorithm

Source: Internet
Author: User

Because of its complexity of O (LOGN), the binary algorithm has very high efficiency in the actual operation. The idea of binary algorithm is often combined with other algorithms to solve real project problems. For example, the nonlinear equation is rooted.

The idea of a binary algorithm is simple, but it is not easy to write correctly. The error of writing a binary algorithm is often not due to negligence, but because the algorithm is too flexible to be hidden. The program crashes, the machine stops, and the weight can cause a fatal loss.

The Error program and sample analysis are given below.

Error 1:

intBsearch (int *a,int x,int y,intV) {int m     while(x<y) {int m=x+ (y-x) /2;if(v = = a[m])return m;Else if(a[m] > V)y=m;Else            x=m; }return-1;}

Suppose x = 2,y = 3,a[3] = = v. At this point the program goes into a dead loop because M is always equal to 2;a[2] < v always x = m; so x is always less than Y.

Error 2:

intBsearch (int *a,int x,int y,intV) {int m     while(x<y) {int m=x+ (y-x) /2;if(v = = a[m])return m;Else if(a[m] > V)y=m-1;Else            x=m+1; }return-1;}

Suppose x = 0,y = 4,a[1] = = v. This time the program will return 1 instead of 1. After operation m = = 2, and because a[2] > V, so execute y = m-1. At this time y = 1, the cycle starts again, after the operation m = = 0, after the following judgment sentence to get x = = 1, this time the loop out, ignoring the M = = 1 case, the final result is wrong.
The correct program for error 1 should be:

intBsearch (int *a,int x,int y,intV) {int m     while(x<y) {int m=x+ (y-x) /2;if(v = = a[m])return m;Else if(a[m] > V)y=m;Else            x=m+1; }return-1; }

The reason here is x = m+1, not y = m-1, is that the M-value is always tending to the minimum when the adjacent integer x, y is obtained by M = x + (y + x)/2 operation. For example, x = 2,y = 3, and finally get m = = 2.
The correct program for error 2 should be:

intBsearch (int *a,int x,int y,intV) {int m     while(x<=y) {int m=x+ (y-x) /2;if(v = = a[m])return m;Else if(a[m] > V)y=m-1;Else            x=m+1; }return-1;}

In view of the above error 2 situation, the x < y is modified to x <= y, when x = = 1, y = = 1 when the loop does not end, but to continue the operation to get m = = 1 case, this time can return the correct solution.

A summary of the binary algorithm

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.