Held: Tuesday, 21 January 2003
Today we will consider the binary search algorithm (and your implementations
of that algorithm) in some detail.
- Binary Search
- Logic and More
- Warning! I am going to savage your code in this discussion.!
- Eight people out of nine turned it in.
- 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
- 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.
- 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
- Doesn't stop at the correct time (e.g., stops when
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.
- Many of you decided to test your binary search. Here's a typical
- 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)
else if (odd(t) and binarySearch(n, x, t) != -1)
- 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.)
- 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
- 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
- How will we write our assertions? In the language of Boolean Logic.
- That is, we will write expressions whose values are either true
- What questions do you have on Gries, Chapters 0 and 1?
Tuesday, 7 January 2003 [Samuel A. Rebelsky]
- Created generic version to set up course.