Fancy Mouse
- -|||
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.
posted on 2008-01-22 22:07  Fancy Mouse  阅读(603)  评论(0编辑  收藏  举报