算法导论8-5思考题-平均排序-average sorting


Suppose that, instead of sorting an array, we just require that the elements increase on average. More precisely, we call an n-element array A k-sortedif, for all i = 1, 2, . . ., n - k, the following holds:

  1. What does it mean for an array to be 1-sorted?

  2. Give a permutation of the numbers 1, 2, . . ., 10 that is 2-sorted, but not sorted.

  3. Prove that an n-element array is k-sorted if and only if A[i A[i + k] for all i = 1, 2, . . ., n - k.

  4. Give an algorithm that k-sorts an n-element array in O(n lg(n/k)) time.

We can also show a lower bound on the time to produce a k-sorted array, when k is a constant.

  1. Show that a k-sorted array of length n can be sorted in O(n lg k) time. (Hint: Use the solution to Exercise 6.5-8.)

  2. Show that when k is a constant, it requires Θ(n lg n) time to k-sort an n-element array. (Hint: Use the solution to the previous part along with the lower bound on comparison sorts.)

解答:
a. 如果是1-排序的,其实就是正常的排序的意思
b.6,2,7,3,8,4,9,5,10
c. 直接从定义推就可以了
d. 要求对一个给定的数组进行k-排序。根据c中的结论,我们只需要正常排序几个整数集合,他们分别是
    1. a[0],a[k],a[2*k],....
    2. a[1],a[1+k],a[1+2*k],...
    3. a[2],a[2+k],a[2+2*k]...
    .....
    k. a[k-1],a[k-1+k],a[k-1+2*k]...
排序每个集合需要O(n/k *log(n/k)) ,所有的共O(n*log(n/k))
e. 这个题说,假设数组已经是k-排序了,如何在O(n log(k))内把该数组正常排序
    这个就是k个有序表merge的过程了,可以使用一个堆来搞,这样就可以了
f. 略

你可能感兴趣的:(算法导论,sorting,算法,permutation,algorithm,numbers,merge)