算法导论 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

Start example

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.
  1. Argue that, if n = length[A], then STOOGE-SORT(A, 1, length[A]) correctly sorts the input array A[1 ‥ n].

  2. Give a recurrence for the worst-case running time of STOOGE-SORT and a tight asymptotic (Θ-notation) bound on the worst-case running time.

  3. 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

Start example

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 pq + 1
  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.

  1. Describe a scenario in which the stack depth of QUICKSORT' is Θ(n) on an n-element input array.

  2. 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

Start example

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 aibi. The goal is to fuzzy-sort these intervals, i.e., produce a permutation 〈i1, i2,..., in〉 of the intervals such that there exist , satisfying c1c2 ≤ ··· ≤ cn.

  1. 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.)

  2. 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

转载于:https://www.cnblogs.com/nocooldown/archive/2010/07/22/1778591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值