Você está na página 1de 4

CS 4102 Homework 3 Solutions

Due Monday, February 18, 2013 at 11:55pm


1. (Problem 6.1-2). Show that an n-element heap has height
]
n lg . Give a mathematical
proof rather than just an explanation. You must prove it in general, not just with an
example. You can just prove it directly, you do not need to use induction. Hint: Consider
the minimum and maximum number of elements in a heap.
The number of elements of a heap ranges from
h
2
to
1 2
1

+ h
.
Then
1 1
2 1 2 2
+ +
<
h h h
n
So
1
2 2
+
<
h h
n
Take the lg of each part:
1
2 lg lg 2 lg
+
<
h h
n
So
1 lg + < h n h
lg n is squeezed between two integers, therefore ]
n h lg
.
2. (Problem 7.2-1). Prove that the recurrence T(n) = T(n-1) + (n) has the solution T(n) =
(n
2
). Hint: Use the unraveling method shown in class.
T(n) = T(n-1) + n
= T(n-2) + n-1 + n
= T(n-3) + n-2 + n-1 + n
= ...
= 1 + 2 + ... + n
= n(n+1)/2
= (n
2
+ n)/2
which is (n
2
).
3. (Problem 7.1-1). Using Figure 7.1 as a model, illustrate the operation of PARTITION on
the array A = {13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11}. Show the contents of the array after
each step of the algorithm.
A = {13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11}
A = {13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11}
A = {9, 19, 13, 5, 12, 8, 7, 4, 21, 2, 6, 11}
A = {9, 5, 13, 19, 12, 8, 7, 4, 21, 2, 6, 11}
A = {9, 5, 13, 19, 12, 8, 7, 4, 21, 2, 6, 11}
A = {9, 5, 8, 19, 12, 13, 7, 4, 21, 2, 6, 11}
A = {9, 5, 8, 7, 12, 13, 19, 4, 21, 2, 6, 11}
A = {9, 5, 8, 7, 4, 13, 19, 12, 21, 2, 6, 11}
A = {9, 5, 8, 7, 4, 13, 19, 12, 21, 2, 6, 11}
A = {9, 5, 8, 7, 4, 2, 19, 12, 21, 13, 6, 11}
A = {9, 5, 8, 7, 4, 2, 6, 12, 21, 13, 19, 11}
A = {9, 5, 8, 7, 4, 2, 6, 11, 12, 21, 13, 19}
4. (Problem 7.1-2, 7.2-2). What value of q does PARTITION return when all elements in
the array A[p..r] have the same value? What is the running time of QUICKSORT when all
elements of array A have the same value? Note: You do not need to answer the second
question of Problem 7.1-2.
PARTITION returns r for the value of q. Running time is (n
2
).
5. (Problem 7-1 parts a and e). Demonstrate the operation of HOARE-PARTITION given
on page 185 of the textbook on the array A = {13, 19, 9, 5, 12, 8, 7, 4, 11, 2, 6, 21}, showing
the values of the array and auxiliary values (i.e., i and j) after each iteration of the while
loop in lines 4-13. Rewrite the QUICKSORT procedure to use HOARE-PARTITION.
Step 1:
A = {13, 19, 9, 5, 12, 8, 7, 4, 11, 2, 6, 21}, i goes from 0 to 1, j goes from 13 to 11.
Step 2:
A = {6, 19, 9, 5, 12, 8, 7, 4, 11, 2, 13, 21}, i goes from 1 to 2, j goes from 11 to 10.
Step 3:
A = {6, 2, 9, 5, 12, 8, 7, 4, 11, 19, 13, 21}, i goes from 2 to 10, j goes from 10 to 9.
Done swapping elements, partition is between elements 9 and 10.
QUICKSORT(A, p, r)
1 if p < r
2 q = HOARE-PARTITION(A, p, r)
3 QUICKSORT(A, p, q)
4 QUICKSORT(A, q+1, r)
6. (Problem 8.1-1). What is the smallest possible depth of a leaf in a decision tree for a
comparison sort?
If n is the number of elements to be sorted, then the smallest possible depth of a leaf is n1.
7. (Problem 8.2-1). Using Figure 8.2 as a model, illustrate the operation of COUNTING-
SORT on the array A = {6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2}. Show the contents of the arrays B and
C at all steps of the algorithm (not just the first few steps).
C = {2, 2, 2, 2, 1, 0, 2}
C = {2, 4, 6, 8, 9, 9, 11}
B = {, , , , , 2, , , , , }
C = {2, 4, 5, 8, 9, 9, 11}
B = {, , , , , 2, , 3, , , }
C = {2, 4, 5, 7, 9, 9, 11}
B = {, , , 1, , 2, , 3, , , }
C = {2, 3, 5, 7, 9, 9, 11}
B = {, , , 1, , 2, , 3, , , 6}
C = {2, 3, 5, 7, 9, 9, 10}
B = {, , , 1, , 2, , 3, 4, , 6}
C = {2, 3, 5, 7, 8, 9, 10}
B = {, , , 1, , 2, 3, 3, 4, , 6}
C = {2, 3, 5, 6, 8, 9, 10}
B = {, , 1, 1, , 2, 3, 3, 4, , 6}
C = {2, 2, 5, 6, 8, 9, 10}
B = {, 0, 1, 1, , 2, 3, 3, 4, , 6}
C = {1, 2, 5, 6, 8, 9, 10}
B = {, 0, 1, 1, 2, 2, 3, 3, 4, , 6}
C = {1, 2, 4, 6, 8, 9, 10}
B = {0, 0, 1, 1, 2, 2, 3, 3, 4, , 6}
C = {0, 2, 4, 6, 8, 9, 10}
B = {0, 0, 1, 1, 2, 2, 3, 3, 4, 6, 6}
C = {0, 2, 4, 6, 8, 9, 9}
8. (Problem 8.3-4). Show how to sort n integers in the range 0 to n
3
-1 in O(n) time. Hint:
Treat each of the n numbers as 3-digit numbers in radix n.
Treat the numbers as 3-digit numbers in radix n. Each digit ranges from 0 to n1. Sort these
3-digit numbers with radix sort, which uses a stable sort like counting sort to sort by each digit.
For counting sort, the array values range from 0 to k. The range of digits is 0 to k as well as 0 to
n-1, so n and k are essentially equal. There are 3 calls to counting sort, each taking (n + k) =
(n + n) = (n) time, so that the total time is (n).
9. (Problem 8.4-2). Explain why the worst-case running time for bucket sort is (n
2
). What
simple change to the algorithm preserves its linear average-case running time and makes
its worst-case running time O(n lg n)?
The worst-case running time for the bucket-sort algorithm occurs when the assumption of
uniformly distributed input does not hold. If, for example, all the input ends up in the first
bucket, then in the insertion sort phase it needs to sort all the input, which takes O(n
2
) time.
A simple change that will preserve the linear expected running time and make the worst-case
running time O(n lg n) is to use a worst-case O(n lg n)-time algorithm, such as merge sort,
instead of insertion sort when sorting the buckets.
10. Suppose that in the algorithm SELECT, the input elements are divided into groups of 7
instead of groups of 5. Develop a recurrence for the worst-case running time T(n) of the
algorithm SELECT in this case. Note: You do not need to solve the recurrence.
Steps 1, 2, and 4 of SELECT take O(n) time.
Step 3 takes 1
) 7 / ( n T
time.
For step 5:
Number of elements greater than the median-of-medians is at least
8
7
2
2
7 2
1
4

,
_

1
1
1

1
1
1

n n
SELECT recurses on at most n (2n/7 8) = 5n/7 + 8 elements.
Recurrence is:
1
) ( ) 8 7 / 5 ( ) 7 / ( ) ( n O n T n T n T + + +

Você também pode gostar