Assignment 06
Contents:
Overview
Topic: Algorithms
Related Reading: Ch. 4.1-4.2 and 4.4-4.6 of [Br].
Due: 8pm Monday, 22 October 2001
Internet Requirements
You will only need an internet connection at the time of submission.
Practice Problems
Problems to be Submitted (20 points)
- (5 points)
Many things in our daily lives involve written instructions (e.g.,
preparing food, setting the clock on the microwave, ordering
a product from a catalog). We want you to look around for such
instructions so that we can see whether these constitute an
algorithm based on the definition given in
Figure 4.1 on page 169 of the Brookshear text.
Find something in your life that contains written instructions
and type these instructions, exactly as they appear (please
select something so that your instructions take up at least a
third of a page, but at most one whole page when typed.)
Now, consider the definition of an algorithm, as given
in Figure 4.1 on page 169 of the Brookshear
text. Evaluate whether or not you feel that the instructions
constitute an "algorithm" by this definition, explaining your
thoughts.
- (5 points)
Suppose the insertion sort as presented in Figure 4.11
(p. 194 [Br]) was applied to the list
Frank
Bob
Carol
George
Alice
Elaine
Describe the organization of the list at the end of each execution of
the body of the outer while structure.
- (5 points)
Imagine that we are searching the list {A, B, C, D, E, F, G}. Please
fill in the following table specifying the number of list items which
are compared to the Target for each search:
target |
sequential search |
binary search |
A |
|
|
B |
|
|
C |
|
|
D |
|
|
E |
|
|
F |
|
|
G |
|
|
- (5 points)
Consider the following sorted list of numbers:
1 |
2 |
9 |
11 |
12 |
13 |
15 |
26 |
23 |
24 |
29 |
34 |
35 |
41 |
43 |
- What sequence of 'middle' values in considered when running the
binary search algorithm (Figure 4.14 of [Br]) with target 6?
- What sequence of 'middle' values in considered when running the
binary search algorithm (Figure 4.14 of [Br]) with target 11?
- What sequence of 'middle' values in considered when running the
binary search algorithm (Figure 4.14 of [Br]) with target 12?
- What sequence of 'middle' values in considered when running the
binary search algorithm (Figure 4.14 of [Br]) with target 19?
- What sequence of 'middle' values in considered when running the
binary search algorithm (Figure 4.14 of [Br]) with target 41?
Overall, please place your answers to all of these questions in a
single document to be submitted.
Extra Credit (2 points)
Read Chapter Review Problem 26 (p. 220 [Br]) which discusses
a puzzle called the Tower of Hanoi. For notation, we will assume that
the three pegs are named A, B, and C, and that the rings are numbered
1, 2, 3, and so on, starting with the smallest ring being 1.
The recursive solution to the problem can be expressed with the
following pseudocode:
procedure Transfer(numRings, startPeg, endPeg, tempPeg)
if (numRings > 0)
then
[
Apply Transfer(numRings-1, startPeg, tempPeg, endPeg)
]
Move single Ring #numRings from startPeg to endPeg
[
Apply Transfer(numRings-1, tempPeg, endPeg, startPeg)
]
Your job is to trace through the various activations of this recursive
algorithm when it is intially called as Transfer(4,A,C,B).
To demonstrate your understanding, your submission should include the
ordered list of single moves which take place during the process.
For example, if we called Transfer(2,A,C,B) the process is:
Move ring #1 from A to B
Move ring #2 from A to C
Move ring #1 from B to C
comp150 Class Page
mhg@cs.luc.edu
Last modified: 11 October 2001