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-sorted if, for all i=1,2,,nk, the following holds:

j=ii+k1A[j]kj=i+1i+kA[j]k.

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

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

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

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

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

e. Show that we can sort a k-sorted array of length n in O(nlgk) time. (Hint: Use the solution to Exercise 6.5-9.)

f. Show that when k is a constant, k-sorting an n-element array requires Ω(nlgn) time. (Hint: Use the solution to the previous part along with the lower bound on comparison sorts.)

a. Ordinary sorting

b. 2,1,4,3,6,5,8,7,10,9.

c.

j=ii+k1A[j]kj=i+1i+kA[j]kj=ii+k1A[j]j=i+1i+kA[j]A[i]A[i+k].

d. Shell-Sort, i.e., We split the n-element array into k part. For each part, we use Insertion-Sort (or Quicksort) to sort in O(n/klg(n/k)) time. Therefore, the total running time is kO(n/klg(n/k))=O(nlg(n/k)).

e. Using a heap, we can sort a k-sorted array of length n in O(nlgk) time. (The height of the heap is lgk, the solution to Exercise 6.5-9.)

f. The lower bound of sorting each part is Ω(n/klg(n/k)), so the total lower bound is Θ(nlg(n/k)). Since k is a constant, therefore Θ(nlg(n/k))=Ω(nlgn).


0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn
0594codes.cn