No., No.-Almost all binary search and mergesort have errors.

Source: Internet
Author: User

This is a post posted on Google blog by Joshua BLOCH (author of valid Java. Before talking about this post, I have to repeat Joshua Bloch's suggestion: if you have not readProgramming pearls(The Chinese version is called programming Pearl.) read this book now. If you read it once, read it again now.

Or back to Joshua's article. Jon Bentley, the author of programming pearls, gave a lecture on CMU. He asked the doctoral students in the computer department to write binary search algorithms, and then analyzed one of them on the spot. Of course, that algorithm and the algorithms written by most people are all wrong. Jon Bentley also mentioned in programming pearls that although binary search was published in 1946, it was not written until the first correctly executed Algorithm in 1962. The key lesson in this story is to carefully consider the invariant algorithm when writing a program ). If I remember it correctly, Chapter 4th of programming pearls explains how to prove the correctness of binary search. Of course, every textbook of discrete mathematics will teach us to list pre-condition, invariant, and post-condition. It proves that pre-condition was established before the cycle started, and invariant was always established in the cycle, post-condition is satisfied after the loop ends, and almost every textbook (at least I have read) uses binary search as an example. So if you are interested, go and check it out.

The binary search code in JDK is implemented in this way (written by Joshua Bloch)

1:     public static int binarySearch(int[] a, int key) {
2: int low = 0;
3: int high = a.length - 1;
4:
5: while (low <= high) {
6: int mid = (low + high) / 2;
7: int midVal = a[mid];
8:
9: if (midVal < key)
10: low = mid + 1;
11: else if (midVal > key)
12: high = mid - 1;
13: else
14: return mid; // key found
15: }
16: return -(low + 1); // key not found.
17: }

The error is in row 6th:
6: int mid = (low + high)/2;
The problem in this line is that when the sum of low and high exceeds 2 ^ 31-1, that is, the maximum integer in Java, the integer overflow occurs, and the mid value becomes a negative number, so the JVM went crazy, so arrayindexoutofboundsexception went on.

If an array contains more than 2 ^ 30 elements, this error is detected. The large array was hard to imagine when it was written in the first version of programming pearls in 1980s, but it is very common now. Therefore, despite the advent of the correct binary search in 1962, the reality is that binary search in popular systems still has errors.

The solution is not difficult. Rewrite the 6th rows

6: int mid = low + (high-low)/2 );

Or
6: int mid = (low + high) >>> 1;

C and C ++ do not have this ">>>", we can do this:

6: int mid = (unsigned) (low + high)> 1.

So is binary search completely correct now? We still don't know. The profound lesson we have learned is that it is not enough to prove that a program is correct. We must test it carefully.GartnerInPeter Van emde BoasThe letter said, "the above section of the program may be wrong. I only proved that it is correct, but it has not been tested ". People often use this passage to demonstrate the meticulous and academic spirit of Gartner. Who knows the deep insight behind this sentence. It is often said that "in theory, there is no difference between practice and theory. In practice, the two are indeed different.

This error of binary search will also appear in other "divide and conquer" algorithms, such as mergesort. If you have similar algorithm code, modify it. Joshua said that the lesson he learned is humility: even a simple program is hard to write, but the whole society runs on a large and complex code.

The final conclusion is very interesting: Our programmers need a variety of help, and there is no other way. Well designed. The test is good. The formal method is good (but I still think there are professors who need to study the formal e-commerce (for example, the model theory), and there is nothing to find ). The Code review is good, and the static analysis is good. But they cannot help us eliminate code errors completely-they will always exist. We have tried our best to eliminate a program error for half a century. We must be careful, defensive, and vigilant.

 

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.