Exercises
6.4-3 What is the running time of heapsort on an array A of length n that is already sorted in increasing order? What about decreasing order?
See 6.4-5
6.4-5 Show that when all elements are distinct, the best-case running time of heapsort is Ω(nlogn)
Since all the leaves have to rise from the bottom level to the top level to be poped, they all have to exchange approximately logn times.
Thus there are approximately nlogn exchanges in all, which makes the Ω(nlogn) conclusion.
Problems
6-3 Young tableaus
An m*n Young tableau is an m*n matrix such that the entries of each row are in sorted order from left to right and the entries of each column are in sorted order from top to bottom.
c. Give an algorithm to implement EXTRACT-MIN on a nonempty m*n Young tableau that runs in O(m+n) time.
EXTRACT-MIN(Tableau A, int C, int R)
A[x,y] = min(A[C+1,R],A[C,R+1])
Result = A[x,y]
Exchange(A[C,R],A[x,y])
EXTRACT-MIN(x,y)
return Result
Call EXTRACT-MIN(A,1,1) for extracting the minimum of the original tableau A.
Since the calling EXTRACT-MIN(A,C,R), C and R are increasing respectively, the overall number of calling is m+n.
Thus the time complexity is O(m+n)
d. Show how to insert a new element into a nonfull m*n Young tableau in O(m+n) time.
INSERT(Tableau A, int C, int R, int value)
if(A[C,R] is empty) A[C,R] = value, return
if(A[C,R] > value)
{
temp = A[C,R]
A[C,R] = value
INSERT(A,C,R+1,temp)
}
else
INSERT(A,C+1,R,value)
Call INSERT(A,1,1,value) for inserting (int)value into the tableau A.
e. Using no other sorting method as a subroutine, show how to use an n*n Young tableau to sort n^2 numbers in O(n^3) time.
n INSERT operations and n EXTRACT-MIN operations makes the O(n^3) sorting algorithm.
转载于:https://www.cnblogs.com/FancyMouse/articles/1049116.html