Chapter 6 Exercises and Problems

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>