Assignment
Contents:
Overview
Topic: Algorithms
Related Reading: Ch. 6.1, 9.4, 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)
- (4 points)
Exercise 12 of Ch. 9 (p. 314)
[Note: please ignore the value "length" which is shown as 8. I do
not know why they say 8 as opposed to 11, since there are 11 items.]
- (5 points)
Exercise 13 of Ch. 9 (p. 314)
Though it does not
state this explicitly, please answer this question in reference to the
Quicksort algorithm.
Also, please note that you only need
to simulate the algorithm until just before the first
recursive call is made (namely, before the statement "Quicksort the
left half" shown on p. 292. That is, I want you to do the
partitioning (described at bottom of p. 293) but then to stop and
show me the state of affairs.
- (5 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?
- (3 points)
What value or sequence of values will be printed when running the
following recursive routine, called originally as
TestA(1):
TestA(Count)
if (Count<5) then
print value of "Count"
call TestA(Count+1)
endif
- (3 points)
What value or sequence of values will be printed when running the
following recursive routine, called originally as
TestB(1):
TestB(Count)
if (Count<5) then
call TestB(Count+1)
print value of "Count"
endif
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:
Towers(n,A,B,C)
if (n>0) then
Call Towers(n-1,A,C,B)
Print "Move ring <n> from <A> to <C>"
Call Towers(n-1,B,A,C)
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: 12 November 2002