For the complete documentation index, see llms.txt. This page is also available as Markdown.

8. Sorting in Linear Time

Exercises

8.1-1

The smallest possible depth of a leaf in a decision tree for a comparison sort is nβˆ’1n-1, where nn is the number of elements to sort. This is equivalent to the best-case number of comparisons needed to sort nn elements. For example, insertion sort will make this number of comparison when given an already sorted array on input.

8.1-2

Available in the latest revision of the IM.

8.1-3

Available in the latest revision of the IM.

8.1-4

Available in the latest revision of the IM.

8.2-1

// The array A and the auxiliary array C after line 5.
A[1:11]=[6,0,2,0,1,3,4,6,1,3,2]
C[0:6]=[2,2,2,2,1,0,2]

// The array C after line 8.
C[0:6]=[2,4,6,8,9,9,11]

// The output array B and the auxiliary array C after one, two, and 
// three iterations of the loop in lines 11–13, respectively. 
// Only the non-X elements of array B have been filled in.
B[1:11]=[X,X,X,X,X,2,X,X,X,X,X]
C[0:6]=[2,4,5,8,9,9,11]

B[1:11]=[X,X,X,X,X,2,X,3,X,X,X]
C[0:6]=[2,4,5,7,9,9,11]

B[1:11]=[X,X,X,1,X,2,X,3,X,X,X]
C[0:6]=[2,3,5,7,9,9,11]

// The final sorted output array B.
B[1:11]=[0,0,1,1,2,2,3,3,4,6,6]

8.2-2

Consider the case where positions i<ji < j each contain the same value vv. Focusing on lines 11 through 13 of Counting-Sort, which fill the output array, we observe that the loop will process A[j]A[j] prior to A[i]A[i]. In this situation, A[j]A[j] is placed into position p=C[v]p = C[v] of BB. Following this placement, line 13 decrements C[v]C[v], ensuring that when the loop later processes A[i]A[i], the value of C[v]C[v] is less than pp. Consequently, A[i]A[i] is inserted into an earlier location in the output array, thereby demonstrating the stability of the algorithm.

8.2-3

Available in the latest revision of the IM.

8.2-4

Available in the latest revision of the IM.

8.2-5

Available in the latest revision of the IM.

8.2-6

Available in the latest revision of the IM.

8.2-7

Available in the latest revision of the IM.

8.3-1

8.3-2

Available in the latest revision of the IM.

8.3-3

Available in the latest revision of the IM.

8.3-4 🌟

Available in the latest revision of the IM.

8.3-5

Available in the latest revision of the IM.

β˜… 8.3-6

Each sorting pass corresponds to feeding a pile of cards into the machine to be sorted into 10 bins based on the current digit. The total number of sorting passes in the worst-case (every possible combination of digits exists) is S(d)=βˆ‘i=0dβˆ’110i=10dβˆ’19S(d)=\sum_{i=0}^{d-1} 10^i=\frac{10^d-1}{9}.

The phrase "keep track of" is slightly ambiguous, so we cover two possible interpretations:

  • The total number of intermediate piles produced during the whole process is commensurate with the number of sorting passes. The cards in 9 of the 10 bins must be put aside to sort each of the bins, except when sorting on the least significant digit position. We have P(d)=9(S(d)βˆ’10dβˆ’1)=10dβˆ’1βˆ’1P(d)=9(S(d)-10^{d-1})=10^{d-1}-1.

  • If we are interested in to estimate the space needed for storing those intermediate piles at any given moment, then we have P(d)=9(dβˆ’1)+1P(d)=9(d-1)+1, where +1 designates the pile being placed into a machine for sorting.

8.4-1

Available in the latest revision of the IM.

8.4-2

Available in the latest revision of the IM.

8.4-3

Available in the latest revision of the IM.

8.4-4

The idea is to split buckets into 2 layers. This works, since n>10n>10 implies that yi/n<⌊10xiβŒ‹/10y_i/n<\lfloor10x_i\rfloor/10. Below is the implementation of the modified bucket sort in Python 3. For the sake of simplicity, we skip corner cases associated with rounding errors.

β˜… 8.4-5

Available in the latest revision of the IM.

β˜… 8.4-6 🌟

The algorithm has two major steps based on bucket sort.

1

Initialize

Create nn buckets. For i=1,2,…,ni=1,2,\dots,n place XiX_i into bucket ⌊P(Xi)nβŒ‹\lfloor P(X_i)n\rfloor. This stage requires O(n)O(n) time. The fact that Yi=P(Xi)Y_i=P(X_i) has a uniform distribution over [0,1)[0,1), due to the probability integral transform, ensures that each bucket's expected size is O(1)O(1).

2

Sort and Concatenate

Because P(x)P(x) is a valid probability distribution function, it is monotonically increasing. This means that XA<XBβ€…β€ŠβŸΉβ€…β€ŠP(XA)≀P(XB)X_A < X_B \implies P(X_A) \le P(X_B). Therefore, grouping the elements by their transformed values P(Xi)P(X_i) guarantees they are also ordered by their original values XiX_i. Sort the elements within each individual bucket. Finally, concatenate the sorted buckets to produce the final sorted array.

Problems

8-1 Probabilistic lower bounds on comparison sorting

Available in the latest revision of the IM.

8-2 Sorting in place in linear time

Available in the latest revision of the IM.

8-3 Sorting variable-length items

Available in the latest revision of the IM.

Wikipedia examines this problem in some detail in its article about radix sort.

8-4 Water jugs

Available in the latest revision of the IM.

8-5 Average sorting

a.

It is equivalent to be sorted in usual way.

b.

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

c.

We first prove the "only if" direction. For all i=1,2,…,nβˆ’ki=1,2,\dots,n-k we have

βˆ‘j=ii+kβˆ’1A[j]kβ‰€βˆ‘j=i+1i+kA[j]kβˆ‘j=ii+kβˆ’1A[j]β‰€βˆ‘j=i+1i+kA[j](eliminatingΒ kΒ fromΒ bothΒ sides)A[i]≀A[i+k](cancellingΒ equalΒ termsΒ onΒ bothΒ sides).\begin{align*} \frac{\sum_{j=i}^{i+k-1}A[j]}{k} &\le \frac{\sum_{j=i+1}^{i+k}A[j]}{k} \\ \sum_{j=i}^{i+k-1}A[j] &\le \sum_{j=i+1}^{i+k}A[j] && \text{(eliminating $k$ from both sides)} \\ A[i] &\le A[i+k] && \text{(cancelling equal terms on both sides)}. \end{align*}

The "if" direction is simply an inverse of the previous steps.

d.

By part (c) we can split the input numbers into k independent chains and sort them separately. Each chain has n/kn/k elements. For example, the first chain begins as A[1],A[1+k],A[1+2k],...A[1],A[1+k],A[1+2k],... The time to sort a chain is O((n/k)lg⁑(n/k))O((n/k) \lg (n/k)) using heap sort or merge sort. The total time for all kk chains is O(nlg⁑(n/k))O(n\lg(n/k)).

e.

Following the hint from the book, we apply the kk-way merge algorithm, since a kk-sorted array can be regarded as having kk sorted lists, that need to be combined (merged) into one sorted output.

f.

Let T(n)T(n) be the time it takes to kk-sort an array of nn elements. To 1-sort an array of nn elements, we can first kk-sort it and then employ the algorithm from part (e). Since we know the lower bound on comparison sorts, this gives

T(n)+O(nlg⁑k)=T(n)+O(n)=Ω(nlg⁑n).T(n) + O(n \lg k)=T(n)+O(n) = \Omega(n \lg n).

Recall that k>1k>1 is a constant. Therefore, we must have T(n)=Ω(nlg⁑n)T(n)=\Omega(n \lg n).

8-6 Lower bound on merging sorted lists

a.

This equals the number of ways to pick nn items out of 2n2n items. Observe that once we select the first batch of nn numbers the second one is automatically determined. We have (see Exercise C.1-13)

(2nn)=22nΟ€n(1+Oβ€²(1/n)).\binom{2n}{n}=\cfrac {2^{2n}}{\sqrt{\pi n}}(1+O'(1/n)).

b.

The following inequalities must hold, where ll is the number of leaves in a decision tree of height hh.

22nΟ€n(1+Oβ€²(1/n))≀l≀2h.\cfrac {2^{2n}}{\sqrt{\pi n}}(1+O'(1/n)) \le l \le 2^h.

Taking logarithms on both sides, we get

hβ‰₯lg⁑(22nΟ€n(1+Oβ€²(1/n)))=lg⁑22nβˆ’lg⁑πn+lg⁑(1+Oβ€²(1/n))=2nβˆ’o(n).\begin{align*} h &\ge \lg \left({\cfrac {2^{2n}}{\sqrt{\pi n}}(1+O'(1/n))}\right) \\ &= \lg 2^{2n} - \lg \sqrt{\pi n} + \lg (1+O'(1/n)) \\ &= 2n-o(n). \end{align*}

c.

Suppose ai≠bja_i \neq b_j. We know that the inequalities below are all satisfied, since aia_i and bjb_j are from different sorted lists and they are consecutive in the sorted order.

aiβˆ’1<ai,bj<ai+1bjβˆ’1<ai,bj<bj+1a_{i-1} < a_i ,b_j < a_{i+1} \\ b_{j-1} < a_i ,b_j < b_{j+1}

We could exchange aia_i and bjb_j without violating any of the constraints above. No matter how many other comparisons are made, none of them can add any new information about the relative order of aia_i and bjb_j. Therefore, aia_i and bjb_j must be compared.

d.

The worst-case number of comparisons happens when elements are taken in alternating manner from sublists during the merge operation, hence, consecutive elements in the sorted order are from different lists. By part (c) they must be compared and we have 2nβˆ’12n-1 such pairs.

8-7 The 0-1 sorting lemma and columnsort

Available in the latest revision of the IM.

Last updated