Description
1. (7 points) Heapsort
Show the array A after the algorithm Min-Heap-Insert(A, 6) is operates on the Min Heap implemented in
array A=[6, 8, 9, 10, 12, 16, 15, 13, 14, 19, 18, 17]. In order to solve this problem you have to do some of
the thinking assignment on the Ch.6 lecture slides. But you do not have to submit your solutions to
those thinking assignments. Use your solutions to determine the answer to this question and provide
the array A below.
A=[ ]
2. (22 points) Quicksort
(a) (6 points)
Quicksort can be modified to obtain an elegant and efficient linear (O(n)) algorithm QuickSelect for the
selection problem.
Quickselect(A, p, r, k)
{p & r – starting and ending indexes; to find k-th smallest number in non-empty array A; 1≤k≤(r-p+1)}
1 if p=r then return A[p]
else
2 q=Partition(A,p,r) {Partition is the algorithm discussed in class}
3 pivotDistance=q-p+1
4 if k=pivotDistance then
5 return A[q]
6 else if k<pivotDistance then
7 return Quickselect(A,p,q─1,k)
else
8 return Quickselect(A,q+1,r, k-pivotDistance)
Draw the recursion tree of this algorithm for inputs A=[10, 3, 9, 4, 8, 5, 7, 6], p=1, r=8, k=2. At each nonbase case node show all of the following: (1) values of all parameters: input array A, p, r & k; (2) A after
Partition. At each base case node show values of all parameters: input array A, p, r & k. Beside each
downward arrow connecting a parent execution to a child recursive execution, show the value returned
upwards by the child execution.
(b) (16 points). This algorithm has two base cases.
Explain what the first base case that the algorithm checks for is, in plain English:
List the steps that the algorithm will execute if the input happens to be this base case:
Complete the recurrence relation using actual constants:
T(first base case) = __________________________
Explain what the second base case that the algorithm checks for is, in plain English:
List the steps that the algorithm will execute if the input happens to be this base case:
Complete the recurrence relation using actual constants (assume complexity of Partition to be 20n):
T(second base case) = __________________________
List the steps that the algorithm will execute if the input is not a base case:
Complete the recurrence relation using actual constants (assume complexity of Partition to be 20n and
the worst case input size for the recursive call):
T(n) = __________________________
How will the above recurrence change if you instead assume the best case input size for the recursive
call):
T(n) = __________________________
3. (10 points) Counting Sort
Show the B and C arrays after Counting Sort finishes on the array A [19, 6, 10, 7, 16, 17, 13, 14, 12, 9] if
the input range is 0-19.
4. (5 points) Radix Sort
If Radix Sort is applied to the array of numbers [4567, 3210, 2345, 4321, 5678], show how these
numbers will get rearranged after each of the four passes of the algorithm.
5. (12 points) Bucket Sort
Consider the algorithm in the lecture slides. If length(A)=15 then list the range of input numbers that will
go to each of the buckets 0…14.
Bucket0:
Bucket1:
Bucket2:
Bucket3:
Bucket4:
Bucket5:
Bucket6:
Bucket7:
Bucket8:
Bucket9:
Bucket10:
Bucket11:
Bucket12:
Bucket13:
Bucket14:
Now generalize your answer. If length(A)=n then list the range of input numbers that will go to buckets
0,1,…(n-2), (n-1).
Bucket0:
Bucket1:
Bucket(n-2):
Bucket(n-1):
6. (20 points) Disjoint Set
Assume a Disjoint Set data structure has initially 20 data items with each in its own disjoint set (onenode tree). Show the final result (only show the array P for parts a, b & c below; no need to draw the
trees) of the following sequence of unions (the parameters of the unions specified in this question are
data elements; so assume that the find operation without path compression is applied to the
parameters to determine the sets to be merged): union(16,17), union(18,16), union(19,18),
union(20,19), union(3,4), union(3,5), union(3,6), union(3,10), union(3,11), union(3,12), union(3,13),
union(14,15), union(14,3), union(1,2), union(1,7), union(8,9), union(1,8), union(1,3), union(1,20) when
the unions are:
a. Performed arbitrarily. Make the second tree the child of the root of the first tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
b. Performed by height. If trees have same height, make the 2nd tree the child of the root of the
1st tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
c. Performed by size. If trees have the same size, make the second tree the child of the root of
the first tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
d. For the solution to part a, perform a find with path compression on the deepest node and
show the array P after find finishes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7. (24 points) Binomial Queue
First show the Binomial Queue that results from merging the two BQs below. Then show the result of an
Extract_Max operation on the merged BQ. There may be more than one correct answer.
37
38
27
29 28
33 30
34
31
32
3
5 6
9 7
1
0
4
8
35
36
1
2
11
22