CS3134 Homework #2
Due on October 14, 2004 at 11:00am
There are two parts to this homework: a written component worth
15 points, and a programming assignment worth 10 points. See
the homework submission
instructions on how to hand it in and for important notes on
programming style and structure.
Written questions
Note that these questions are intended to be theoretical unless stated
otherwise. Don't worry about the actual implementation (e.g., arrays,
etc.) - just answer the questions based on how stacks, queues and related
data structures work fundamentally.
- While stacks and queues are rather primitive data structures, they're
surprisingly flexible for solving a large variety of problems.
Interestingly, we can actually use a queue to support a
list-style data structure instead of an array, even though it only has two operations (enqueue
and dequeue). In this problem, we'll explore how we can do this.
While it's not required, you can assume that there's a third operation
supported in our queue for the purposes of this problem: length, which
specifies the number of items currently in the queue. While you
don't need to write code, try and be as precise as you can - a few
clearly-defined sentences, lists of steps or psuedocode should work for
each.
- (1 point) Describe an algorithm to support a list-style (unordered) insert
using a queue.
- (2 points) Describe an algorithm to support a list-style linear search
using a queue.
- (1 point) Given (b), how can we support remove using a queue?
- (1 point) Suggest what the complexities (using big-Oh notation) for
the above three operations are. Explain why, using 1-2 sentences
for each.
- (2 points extra credit) While it's tricky, you can actually
sort the contents of a queue using just the queue and two extra temporary variables.
Describe an algorithm that you can use to do this. (Hint: it's a
little like insertion sort, but with some twists.)
- All three sorts we've covered so far (bubble, insertion, and
selection) are rather inefficient in sorting large arrays.
Let's assume I have a large array of elements ordered
backwards. Sorting such an array takes O(n2) time for all
three, which for large n (say, more than 1000) is rather slow.
There are a number of alternatives we can develop that should
run much faster.
- (1 points) As we described in class, a stack can
be used to reverse items. Explain in a few sentences what
sequence of stack operations would do just this. State,
using big-Oh notation, the worst-case running time for such
an algorithm, and explain why. (Hint:
explain how the combination of many low-level stack
operations "adds up" to such a high-level worst-case
time.)
- (2 points) Describe an alternative algorithm that we can use if we
wanted to only use the one array to store information,
instead of having to create an additional data structure
(like a stack) to manipulate the data. Such an algorithm
might be called in-place - that is, the data elements are
manipulated mostly within the array itself (a few individual external
variables notwithstanding).
- (1 point) Approximate the worst-case running time of
(b), using a similar analysis to (a) (that is, decomposing
the high-level operation into a series of lower-level steps
and combining their respective running times).
- (1 point) Of these two, which is more efficient, and
why? Use 2-3 sentences to explain. An informal analysis of
space and/or time usage should be sufficient. (Note: this
does not imply that the approximate worst-case
running times are necessarily different. They may very well
be equal, but one of the two will still have their
advantages.)
- While we've mostly done sorts with lists so far, sorts can be useful
with other data structures as well. In this experiment, we'll
create an algorithm that enables us to sort a queue using nothing more
than two stacks.
- (3 points) Given a queue filled with unsorted numbers and two
empty stacks, describe an algorithm that will sort the queue (i.e.,
using only the queue and stacks to hold data and only the
appropriate operations for each, describe an algorithm that will result
in a queue full of the sorted numbers, in order, when it's done).
- (1 point) Approximate the worst-case running time of (a).
(Hint: how much "work" do you need to do for each number in the queue?)
- (1 point) What queue configuration would result in the fastest
absolute time for your sort algorithm (i.e., even though (b) refers to
the worst-case time, what configuration would produce the best
scenario?) What queue configuration would result in the worst
absolute time for your sort algorithm?
Programming problem
Since the point of this course is to be able to design algorithms and then
implement them, we're going to take your written answer for #3 and
develop an implementation for it. The book provides both
Stack and Queue
code, which you're welcome to download and use for this exercise by
clicking the appropriate links in this sentence. Before we
implement #2, though, we're going to extend each class's functionality
so that they're growable, and aren't limited to an initial number
of items.
-
(2 points) Make the Stack class
auto-expandable. First, change the constructor that it takes no
parameters and assumes an initial length of 5 items, and remove the
isFull method. Change the book's
insert method so that instead of causing
problems when the stack fills up, the method first checks to make sure the
stack isn't full, and if it is full, creates a new array twice the
size of the old stackArray and copies the
items over. Finally, update the stackArray
reference to the new array.
-
(3 points) Make the Queue class auto-expandable
using a similar strategy as you did in part 1. Be careful that you
maintain the order of the circular queue, although you can rearrange the
location of the front and rear if you want to.
-
(4 points) Create a class called StackSort.
This class will have exactly one method - sort()
- which takes one parameter, a Queue, and
sorts it according to your algorithm in 3(a) from the written portion of the
homework. You will create the two stacks and manipulate them, along
with the supplied Queue, within this class.
The method doesn't need to return anything, since it's manipulating the
original Queue by reference.
-
(1 point) Write a class called StackApp,
which asks the user, line by line, for a new number, and inserts the results
into a Queue. Keep on asking the user
for numbers until the user enters a blank line to terminate input.
Then, have StackApp call the sort method in
StackSort on the
Queue, and finally print out the results of the
Queue by repeatedly dequeueing it.
Notes:
- When implementing parts 1, 2, and 3, name the methods exactly as I
suggested. The TAs may use their own tester code instead of your
StackApp.
- Again, you may find it helpful to test each piece of code
individually - especially when making the Queue
auto-expandable. You can edit and work with the
main() methods in each of those classes,
and can run the appropriate main method by just typing in (for example)
"java Queue" instead of "java
QueueApp".
- You'll note I deliberately left you some more flexibility as to how
to implement this. The tricky part in this assignment is
thinking about the problem, more so than implementing, unlike HW#1.
If necessary, "think" about the problem on paper first before trying to
code it!