Binary Lookup implementation (Jon bentley:90% programmer does not implement correctly)

Source: Internet
Author: User

Binary Lookup implementation (Jon bentley:90% programmer does not implement correctly)
July
Source: The way of the method of structure algorithm
Introduction
Jon bentley:90% The programmer is unable to write out the binary lookup code correctly. Maybe a lot of people have already
I've heard that, but I still want to cite the following paragraphs in programming Zhu Ji Nanxiong:
"Binary lookup solves the problem of (finding a pre-sorted array): As long as the array contains T (that is, the value to look for),
So by shrinking the range that contains T, you can eventually find it. Initially, the scope covers the entire array. The number
The middle of the group is compared to T, which excludes half of the elements and shrinks by half. In this way, repeated comparisons, repeated shrinkage
A small range will eventually find T in the array, or determine that the range originally thought T is actually empty. For a containing n
Elements of the table, the entire lookup process is approximately log (2) N times compared.
Most programmers feel that it is not difficult to write code as long as they understand the above description, but that is not the case. If
You do not agree with this, the best way is to put down the book, write it yourself. Give it a try.
I've had this exam at Bell Labs and IBM. Those professional programmers have a few hours of time,
You can write the above description in the language of their choice, or write high-level pseudo-code. At the end of the exam, almost
All programmers think they have written the right program. So it took us half an hour to look at the code they wrote
The result of a test case validation. In several classes, the results of more than 100 people are similar: 90% of programmers write programs that have
Bug (I don't think the code without bugs is correct).
I was surprised: in enough time, only about 10% of professional programmers could write this applet right. But
There's more to this little program than that: Gartner's 3rd book on the Art of computer programming, sorting and checking
The "History and References" section of section 6th 2.1 indicates that, although in the early 1946, the method of finding binary points was
But it wasn't until 1962 that someone wrote out a binary finder without bugs. "--Joen Bentley,"
Cheng Zhu Ji Nanxiong (1th edition) "第35-36 page.
Can you write the two-point lookup code correctly? Try it.
348
Two-point lookup code
The principle of binary search must not be explained more, but one thing to remind the reader is that the binary search is for
A sorted array. OK, the paper read to the end of shallow, aware of this matter to preach. I'm going to write a copy, and here's a
Binary search implementation (before going to a company interview has been called on the spot to achieve binary search, but the results may be with you
is not completely and correctly written), there are any questions or errors, please do not hesitate to correct me:
Two-point Find V0.1 Implementation version
[Email protected] July
Readers are always welcome to find Bug,email:zhoul[email protected].
The first thing to do is to grasp the following points:
Right=n-1 (left <= right) = right=middle-1;
Right=n = while (left < right) = Right=middle;
The middle calculation cannot be written outside the while loop, or it cannot be updated.
int binary_search (int array[],int n,int value)
{
int left=0;
int right=n-1;
If this is int right = N, then there are two places below that need to be modified to ensure that one by one corresponds:
1. The following loop conditions are while (left < right)
2, in the loop when array[middle]>value, right = mid
while (left<=right)//Cycle condition, timely change
{
int Middle=left + ((right-left) >>1); Prevents spills and shifts more efficiently. At the same time, each cycle
All need to be updated.
if (Array[middle]>value)
{
Right =middle-1; Right assignment, timely and change
}
else if (array[middle]<value)
{
left=middle+1;
}
Else
return middle;
There may be readers who think that the first judgment is equal, but after all the inequality in the array is more
If each cycle is judged equal, it will be time-consuming
}
return-1;
349
}
Under the simple test, the result of the run is as follows (of course, a test correctly does not mean that the program is 0 bugs, and the test
Depth is far from enough):
Test
Maybe you've implemented the binary search many times before, but you might want to test it again. Close all pages,
window, open Notepad, or editor, or directly under the comments of this article, do not refer to the above I wrote or anyone else
program, give yourself 10 minutes to n hours of time, write a binary search program immediately. Standalone disposable
Write it correctly, you can leave the code and email address, I send you a copy of this blog blog collection chm file &&
13 Classic Algorithms study PDF documents with tags + catalogs (you can also go to my resources download:
HTTP://DOWNLOAD.CSDN.NET/USER/V_JULY_V).
350
Of course, it doesn't mean anything to write correctly, not to write correctly or to represent anything, just for Jon.
Bentley's comments are a simple test. In the next chapter, see Chapter 26th: Generating inverted based on a given document
Coding and practice of row indexes. Thank you.
Summarize
After the publication of this article, a lot of friends have tried on their own right away. Based on the code left by friends in this article comment,
Find the highest error rates in the following places:
1. The comments have made it clear that there are still many friends who make such mistakes:
1.//The following points must be grasped first:
2.//right=n-1 (left <= right) = right=middle-1;
3.//right=n (Left < right) = Right=middle;
4. The//middle calculation cannot be written outside the while loop, otherwise it cannot be updated.
2. One of the most common mistakes is @ potatoes:
Middle= (left+right) >>1; In this case, the left and right values are larger when they are and may overflow.
You continue to work hard.
Updated: Gentlemen, you can get here 0 points download the latest blog post of this blog the 6th phase CHM file:
http://download.csdn.net/detail/v_july_v/4020172.

Binary Lookup implementation (Jon bentley:90% programmer does not implement correctly)

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.