算法导论 chapter7 快排
problem 7.1 and 7.5
才知道快速排序也有不少版本。 版本不同之处在于partition的算法。以前大学学的是使用Median-of-3 + Hoare partition(左右交换法)。导论上首先给出的算法就叫左右界限推算法吧。基本就是选取目标元素(最后一个),然后从左到右和目标值比较,半段属于左边还是右边(用两个索引值表示左右两个集合的界限)
简单实现一下两种算法:
c++:
/Files/nocooldown/quicksort.zip
problem 7-2:Alternative quicksort analysis
pass
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 ← ⌊(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.
-
Argue that, if n = length[A], then STOOGE-SORT(A, 1, length[A]) correctly sorts the input array A[1 ‥ n].
-
Give a recurrence for the worst-case running time of STOOGE-SORT and a tight asymptotic (Θ-notation) bound on the worst-case running time.
-
Compare the worst-case running time of STOOGE-SORT with that of insertion sort, merge sort, heapsort, and quicksort. Do the professors deserve tenure?
answer:
a. easy to prove that stooge-sort(2) works. then prove when stooge-sort(n) works => stooge-sort(n+1) .
b. T(n) = 3T(2n/3) + O(1), using master theory => T(n) = O(n*n)
c. pass
Problems 7-4: Stack depth for quicksort
The QUICKSORT algorithm of Section 7.1 contains two recursive calls to itself. After the call to PARTITION, the left subarray is recursively sorted and then the right subarray is recursively sorted. The second recursive call in QUICKSORT is not really necessary; it can be avoided by using an iterative control structure. This technique, called tail recursion, is provided automatically by good compilers. Consider the following version of quicksort, which simulates tail recursion.
QUICKSORT'(A, p, r)
1 while p < r
2 do ▸ Partition and sort left subarray.
3 q ← PARTITION(A, p, r)
4 QUICKSORT'(A, p, q - 1)
5 p ← q + 1
-
Argue that QUICKSORT'(A, 1, length[A]) correctly sorts the array A.
Compilers usually execute recursive procedures by using a stack that contains pertinent information, including the parameter values, for each recursive call. The information for the most recent call is at the top of the stack, and the information for the initial call is at the bottom. When a procedure is invoked, its information is pushed onto the stack; when it terminates, its information is popped. Since we assume that array parameters are represented by pointers, the information for each procedure call on the stack requires O(1) stack space. The stack depth is the maximum amount of stack space used at any time during a computation.
-
Describe a scenario in which the stack depth of QUICKSORT' is Θ(n) on an n-element input array.
-
Modify the code for QUICKSORT' so that the worst-case stack depth is Θ(lg n). Maintain the O(n lg n) expected running time of the algorithm.
answer:
a. pass
b. every time partition(A, p, r) returns r-1, this is the worst case
c. using the original algorithm, the worst case is like what I said in b. if we change the algorithm to sort the smaller subarray each time, the case in b is no longer the worst case. it becomes a best case for stack depth as the stack depth is at most 2. Using this algothrim, the worst case for stack depth is when each time, sort on a size/2 array. so the depth is Θ(lg n)
Problems 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 〈i1, i2,..., in〉 of the intervals such that there exist , satisfying c1 ≤ c2 ≤ ··· ≤ cn.
-
Design an algorithm for fuzzy-sorting n intervals. Your algorithm should have the general structure of an algorithm that quicksorts the left endpoints (the ai 's), but it should take advantage of overlapping intervals to improve the running time. (As the intervals overlap more and more, the problem of fuzzy-sorting the intervals gets easier and easier. Your algorithm should take advantage of such overlapping, to the extent that it exists.)
-
Argue that your algorithm runs in expected time Θ(n lg n) in general, but runs in expected time Θ(n) when all of the intervals overlap (i.e., when there exists a value x such that x ∈ [ai, bi] for all i). Your algorithm should not be checking for this case explicitly; rather, its performance should naturally improve as the amount of overlap increases.
c++ code:/Files/nocooldown/fuzzysort.zip
posted on 2010-07-22 11:50 freestyleking 阅读(961) 评论(0) 编辑 收藏 举报