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 , where is the number of elements to sort. This is equivalent to the best-case number of comparisons needed to sort 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 each contain the same value . Focusing on lines 11 through 13 of Counting-Sort, which fill the output array, we observe that the loop will process prior to . In this situation, is placed into position of . Following this placement, line 13 decrements , ensuring that when the loop later processes , the value of is less than . Consequently, 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 .
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 .
If we are interested in to estimate the space needed for storing those intermediate piles at any given moment, then we have , 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 implies that . 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.
Sort and Concatenate
Because is a valid probability distribution function, it is monotonically increasing. This means that . Therefore, grouping the elements by their transformed values guarantees they are also ordered by their original values . 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.
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 we have
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 elements. For example, the first chain begins as The time to sort a chain is using heap sort or merge sort. The total time for all chains is .
e.
Following the hint from the book, we apply the -way merge algorithm, since a -sorted array can be regarded as having sorted lists, that need to be combined (merged) into one sorted output.
f.
Let be the time it takes to -sort an array of elements. To 1-sort an array of elements, we can first -sort it and then employ the algorithm from part (e). Since we know the lower bound on comparison sorts, this gives
Recall that is a constant. Therefore, we must have .
8-6 Lower bound on merging sorted lists
a.
This equals the number of ways to pick items out of items. Observe that once we select the first batch of numbers the second one is automatically determined. We have (see Exercise C.1-13)
b.
The following inequalities must hold, where is the number of leaves in a decision tree of height .
Taking logarithms on both sides, we get
c.
Suppose . We know that the inequalities below are all satisfied, since and are from different sorted lists and they are consecutive in the sorted order.
We could exchange and 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 and . Therefore, and 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 such pairs.
8-7 The 0-1 sorting lemma and columnsort
Available in the latest revision of the IM.
Last updated