Class 02: Binary Search

Back to Introduction. On to Program Verification.

Held: Tuesday, 21 January 2003

Summary: Today we will consider the binary search algorithm (and your implementations of that algorithm) in some detail.

Due

• Gries 0-1

Overview:

• Binary Search
• Assertions
• Logic and More

Binary Search

• Warning! I am going to savage your code in this discussion.!
• Eight people out of nine turned it in.
• Language:
• One person wrote the program in C.
• Two people wrote the program in Scheme.
• Six people wrote the program in Java.
• A few of those who wrote Java wrote the function with no infrastructure for testing.
• Correctness:
• One out of eight was correct, at least as far as I could tell with a cursory read through.
• Stupid mistakes:
• Didn't handle length-0 vector correctly (see the preconditions).
• Changed the parameters.
• Compared t to indices rather than to elements of x.
• Copied the valid subrange.
• Formatting:
• Other general idiosyncracies
• Lots and lots of special cases.
• Used mid and size rather than lower and upper to keep track of the portion of the array still in consideration.
• Some potential mistakes:
• Doesn't necessarily shrink the array (e.g., using `mid` rather than `mid+1` or `mid-1`).
• Doesn't stop at the correct time (e.g., stops when `lower` and `upper` are equal).
• ...
• Should you feel bad that you're unable to implement a core algorithm algorithm correctly? Somewhat. However, many professional programmers can't do it either (at least not on the first try) and even the published binary searches have been wrong in the past.

Testing Binary Search

• Many of you decided to test your binary search. Here's a typical testing strategy:
• Create a standard array of values.
• Input a value to search for.
• Output the index.
• Are there better ways to test? For example, is there a methodological way of testing your algorithm?
• Certainly. Here's one
• Build lots of different size array.
• Look for every value in the array.
• Look for values not in the array (something smaller than everything, something bigger than everything, something between each pair of values in the array).
• In pseudocode,
```for n = 0 to M
create an array, x, of size n
for i = 1 to n
put 2*i in location i
for t = -1 to 2*n + 1
if (even(t) and binarySearch(n, x, t) != t/2)
reportError(t, n)
else if (odd(t) and binarySearch(n, x, t) != -1)
reportError(t, n)
```

Analyzing Binary Search

• What do we want to guarantee in order to be sure that our binary search is correct?
• Here are two key things:
• The algorithm terminates.
• We never throw away part of the array containing t.
• How can we make sure the algorithm terminates? We need to make sure that we shrink the portion of the array searched at every step.
• What else? We might care that
• All array references are valid. (A problem for many of you.)
• ...

Assertions

• Once we've done more careful analysis of our algorithms, we can spend some time annotating our algorithms with assertions that indicate conditions that we expect to hold at that point in the algorithm.
• For example, to verify that an index is legal, we might compare it to the smallest and largest legal indices.
• Many programming languages let you include such assertions and turn them on (for debugging) and off (for speed).
• We'll spend much of this semester looking at ways to write and use assertions.

Boolean Logic

• How will we write our assertions? In the language of Boolean Logic.
• That is, we will write expressions whose values are either true or false.
• What questions do you have on Gries, Chapters 0 and 1?

History

Tuesday, 7 January 2003 [Samuel A. Rebelsky]

• Created generic version to set up course.

Back to Introduction. On to Program Verification.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri May 2 14:20:07 2003.
The source to the document was last modified on Mon Jan 20 12:41:00 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Outlines/outline.02.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu