Chapter 7 Problems

Problems
7-3 Stooge sort
Professors Howard, Fine, and Howard have proposed the following "elegant" sorting algorithm: STOOGE-SORT(A,i,j)
1 if A[i] > A[j]
2 then exchange A[i] <-> A[j]
3 if i+1 >= j
4 then return
5 k <- floor((j-i+1)/3) //Round down.
6 STOOGE-SORT(A,i,j-k) //First two-thirds.
7 STOOGE-SORT(A,i+k,j) //Last two-thirds.
8 STOOGE-SORT(A,i,j-k) //First two-thirds again.

b. Give a tight asymptotic bound on the worst-case running time.
Solve the recurrence T(n) = 3*T(n*2/3) + Θ(1)
We have T(n) = Θ(n^log(1.5,3)) or Θ(n^2.71) approximately
We can see that though the algorithm is elegant, the efficiency is not.

7.6 Fuzzy sorting of intervals
Consider a sorting problem in which the numbers are not known exactly. Instead, for each number, we know an interval on the real line to which it belongs. That is, we are given n closed intervals of the form [ai,bi], where ai <= bi. The goal is to fuzzy-sort these intervals, i.e., produce a permutation of the intervals such that there exist cj ∈ [aij,bij] satisfying c1 <= c2 <= ... <= cn.
a. Design an algorithm for fuzzy-sorting n intervals.
First select an interval [ax,bx] as a pivot.
Then partition the intervals into three sections: 1. Those whose upper bounds are lower than bx; 2. Those whose lower bounds are higher than ax; 3. Others
Then place these three sections in the order: [1][3][2]
For Section 1 & 2, recursively call the interval-sort respectively.
This algorithm is similar to quicksort, but if all intervals overlap, then Section 1 & 2 will be null. Thus no recursive calls are made, which means the problem is easier.

b. Argue that your algorithm runs in expected time Θ(nlogn) in general, but runs in expected time Θ(n) when all of the intervals overlap. Your algorithm should not be checking for this case explicitly.
This algorithm follows almost the same complexity model of the quicksort's. Thus the Θ(nlogn) bound satisfies for this algorithm.
However, when all the intervals overlap, in the first call of interval-sort, Section 1 & 2 will be null. Then we'll just return the Section 3, and doing nothing to Section 1 & 2(since nothing can be done). The work by this level of interval-sort is Θ(n), thus the overall complexity is Θ(n) for these overlapping intervals.

转载于:https://www.cnblogs.com/FancyMouse/articles/1049259.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值