| CS 261 | University of Puget Sound | Spring, 2020 |
|
Computer Science II
|
||
|
Abstract Data Types and their Implementations,
Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency |
||
Warning:
This course is under development.
Although the basic structure of this course is largely established,
nothing on this Web site should be considered official or even possibly
correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.
The reading on
loop invariants identified four different loop invariants for a
binary search and discussed how two of those invariants could be
used to create a correct implementation of a binary search. The
underlying theme is that a binary search can be implemented if
several correct ways, but also in many, many incorrect ways. The
basic challenge is to consistently set
variables left, right,
and middle, so that always have the same conceptual
interpretation.
Following Version 2 in
the reading on
loop invariants, write a binary-search
method binarySearch2 within
a BinarySearch class, and write tests for this
method for three types of arrays:
Your tests also should include circumstances where the desired item is first in the array, last in the array, and somewhere in the middle.
Since the reading provided considerable analysis for two of the four identified loop invariants, it is natural to consider the details for the other two loop invariants.
Consider the following loop invariant for the binary search:
Write method binarySearch3 that implements this
pictoral loop invariant, and test it with the same arrays and
elements utilized in Step 1.
Repeat Step 2 for the following loop invariant, implementing a
method binarySearch4: