
CS 5633 Analysis of Algorithms { Spring 04
2/11/04
5. Homework
Due
2/18/04
before class
As usual, justify all your answers.
1.
Sorting algorithms (6 points)
Consider the four sorting algorithms
mergesort
,
quicksort
,
heapsort
,
counting sort
.
a) Which of the three algorithms are
stable
?
b) Which of the three algorithms sort
in-place
? (An algorithm sorts
in-place
if
it needs only a constant amount of extra space.)
c) Do you think that it would be possible to slightly modify the algorithms to
make them work in-place or to make them stable?
2.
Decision tree for merge sort (3 points)
Draw the decision tree for merge sort for inputs of length 3.
3.
Leafs in decision trees (4 points)
Consider a decision tree for a comparison sort of
n
elements.
a) How close to the root can a leaf be, i.e., what is the smallest possible depth
(the highestmost possible layer) at which a leaf can be?
b) How far away from the root can a leaf be, i.e., what is the largest p ossible
depth (the lowestmost possible layer) at which a leaf can be?
4.
Median computation (7 points)
Suppose arrays
A
and
B
are
both sorted
and both contain
n
elements. Givea
randomized divide-and-conquer algorithm to ๎nd the median of
A
[
B
in
expected
O
(log
n
) time. (Describe it either in words or as pseudo-code; whatever
you prefer). Argue
shortly
why the runtime is
O
(log
n
).
Hint: Take a look at
randomized select.
4.
Radix (1 point)
How many digits are there when a 64-bit quantity is viewed as a radix-128
number? Describ e how to extract each of the digits.
5.
Ranges (7 points)
Given
n
integers eachbetween 0 and
k
(inclusive). Give an algorithm which
preprocesses the input in
O
(
n
+
k
) time such that the following query can be
answered in
O
(1) time: \How many of the integers are in the range [
a; b
] (where
a; b
are query arguments) ?"
Hint: Take a look at counting sort.
6.
Sort in linear time (2 points)
Given
n
integers in the range 0 to
n
4
๎
1. How fast can you sort them?