Professional Documents
Culture Documents
T02 Problems
T02 Problems
1. Consider the heap implementation of a Priority Queue shown in class that keeps its items
in an Array A[].
3. Your input is k sorted lists that need to be merged into one sorted list. The “obvious”
solution is to modify the merging procedure from mergesort; at every step, compare the
smallest items from each list and move the minimum one among them to the sorted list.
Finding the minimum value requires O(k) time so, if the lists contain n items in total the
full k-way merge would take O(nk) time.
Design an O(n log k)-time algorithm to merge k sorted lists into one sorted list
by making use of priority queues.
Note that each sorted list may contain a different number of elements.
1
6. The following is the pseudo-code for a procedure known as BubbleSort for sorting an
array of n integers in ascending order.
repeat
swapped := false;
for i = 1 to n-1
if A[i] > A[i+1] then
swap A[i] and A[i+1]
swapped := true
until not(swapped)
Max1≤i<n (yi+1 − yi ).
Using a more advanced form of the Θ(n log n) proof of the lower bound for sorting it can be
proven that calculating MAX-GAP requires Θ(n log n) operations if only comparisons and
algebraic calculations are used. In this problem, we will see that, if the floor (truncate)
operator bxc can also be used, the problem can be solved using only O(n) operations!
Prove that this algorithm outputs the correct answer and show that every step can be
implemented in O(n) time (the 3rd step might require the use of the floor function).