Assignment
Contents:
Overview
Topic: Algorithms
Related Reading: Ch. 6.1, 9.5, and pp. 518-525
Due:
Internet Requirements
You may want an Internet connection for several practice
problems. However you do not necessarily need the connection for
completing the assignment, other than for submission.
Practice Problems
Problems to be Submitted (20 points)
Our goal for this homework is to have you demonstrate a good
understanding for the recursive procedure Binary Search, as discussed
in Chapter 9.5. But more generally, we want you to gain an
understanding recursion as a general programming technique.
- (10 points)
Consider the following sorted list of numbers:
2 |
4 |
9 |
16 |
19 |
21 |
25 |
27 |
32 |
33 |
35 |
38 |
41 |
42 |
43 |
- What sequence of 'middle' values are compared to the target
when performing binary search with target 4?
- What sequence of 'middle' values are compared to the target
when performing binary search with target 17?
- What sequence of 'middle' values are compared to the target
when performing binary search with target 22?
- What sequence of 'middle' values are compared to the target
when performing binary search with target 34?
- What sequence of 'middle' values are compared to the target
when performing binary search with target 41?
- (5 points)
What value or sequence of values will be printed when running the
following recursive routine, called originally as TestA(1):
define TestA(count)
if (count<5) then
print count
TestA(count+1)
end
end
- (5 points)
What value or sequence of values will be printed when running the
following recursive routine, called originally as
TestB(1):
define TestB(count)
if (count<5) then
TestB(count+1)
print count
end
end
Overall, please type your answers to all of the problems in a
single document to be submitted electronically. Please see details about the
submission process.
Extra Credit (2 points)
The goal here is to really challenge your understanding of recursive
procedures. A classic problem, known as the
Towers
of Hanoi can be solved with the following recursive procedure:
define Towers(n,A,B,C)
if (n>0) then
Towers(n-1,A,C,B)
Print "Move ring <n> from <A> to <C>"
Towers(n-1,B,A,C)
end
end
To prove your understanding, please describe the precise output which
is generated when a call is made as Towers(4,X,Y,Z).
Last modified: 24 March 2003