90% programmers cannot write bug-free binary lookup programs?

Source: Internet
Author: User
Programming Pearl River (second edition) I mentioned more than 100 majors in Chapter 4 Program The staff spent two hours to write a simple binary search program. The results showed that 90% of people wrote Code All have bugs. knuth also mentioned in his book "sorting and searching" that the first binary search program was published in 1946, however, in 1962, the first bug-free binary lookup program appeared. It took 16 years. So why is a simple binary search program so prone to errors? Take a look at the test case design for the query of ordered tables. You may be able to understand why. To design a case for an ordered Table query, we can analyze the input fields first. In fact, there are two input fields, one is the data to be searched, and the other is the ordered table, you can first classify the number of ordered table data. The ordered table may have ,... Data. Therefore, we can classify the target data into the following categories: After the classification is completed, we can classify the features of the data, because the ordered table is an ordered table, there is a size order, so we can classify the data based on the characteristics of the data. We can use three data as an example to classify the data as follows: when an ordered table contains more than 0, 1, 2, and 4 data records, the table can be reclassified in a similar way. After a sorted table is classified, You can classify it based on the data to be searched. When the queried data and sorted table are divided into different classes, you can combine these two types. For example, you can combine the classification of three data types in an ordered table and the classification of the searched data to get the following classification, you also need to delete some combinations that are not possible or unnecessary. For example, when three data items are equal, finding data between two adjacent data sets does not exist, in this case, you need to delete the three types of data in the ordered table. Because the data in the set is equal, the data is converted into a category, that is, a category with three data equal: in this way, the number of seven final categories is reduced to only four final categories. Not all cases require testing when data is empty, in fact, as long as there are data and no data in the test order table, it is enough. Therefore, if the search data is empty, if there is a classification in other cases, you can also delete it, in this case, when the three data items are equal, there are only three final categories, as shown in: when the order table has 0 data items, we can see two test cases, one is that the data to be searched is empty, and the other is that the data to be searched is not empty. When there is one data in an ordered table, the classification can be reduced to the following three types: the two data types in an ordered table can be reduced to the following eight types, even if more than four pieces of data and three pieces of data are classified under the same conditions as two pieces of data, a total of more than 20 types of final data are classified. Each classification requires at least one test case, A total of at least 20 test cases are required, and a simple binary search test case requires at least 20, you may understand why 90% of professional programmers cannot write a bug-free binary lookup program.

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.