Chapter 7
Exercise 7.1-1
Coloring is similar as in the book, whilst indexing is the same.
13 19 9 5 12 8 7 4 21 2 6 11 13 19 9 5 12 8 7 4 21 2 6 11 13 19 9 5 12 8 7 4 21 2 6 11 9 19 13 5 12 8 7 4 21 2 6 11 9 5 13 19 12 8 7 4 21 2 6 11 9 5 13 19 12 8 7 4 21 2 6 11 9 5 8 19 12 13 7 4 21 2 6 11 9 5 8 7 12 13 19 4 21 2 6 11 9 5 8 7 4 13 19 12 21 2 6 11 9 5 8 7 4 13 19 12 21 2 6 11 9 5 8 7 4 2 19 12 21 13 6 11 9 5 8 7 4 2 6 12 21 13 19 11 9 5 8 7 4 2 6 11 21 13 19 12
Exercise 7.1-2
Available in the latest revision of the IM.
Exercise 7.1-3
Available in the latest revision of the IM.
Exercise 7.1-4
Available in the latest revision of the IM.
Exercise 7.2-1
Available in the latest revision of the IM.
Exercise 7.2-2
Available in the latest revision of the IM.
Exercise 7.2-3
Available in the latest revision of the IM.
Exercise 7.2-4
Available in the latest revision of the IM.
Nearly sorted input will push QuickSort toward the worst-case scenario, rather than the best-case, as hinted in the IM.
Exercise 7.2-5
Available in the latest revision of the IM.
Exercise 7.2-6
Available in the latest revision of the IM.
Exercise 7.3-1
Available in the latest revision of the IM.
Exercise 7.3-2
Available in the latest revision of the IM.
The analysis of the best-case in the IM is a bit inaccurate. To sort an array of elements, Randomized-QuickSort must find the matching permutation of elements, that is, find their sorted positions. Each call to Randomized-Partition fixes the pivot, so leaves are singleton calls to Randomized-QuickSort. In the best-case, the recursion tree is as balanced as possible at every level. The number of pivots matches non-singleton subarrays and equals with most balanced splits, hence we have leaves. Despite ending with the same conclusion, as in the IM, of calls to Random, it is important to be accurate in analysis.
Exercise 7.4-1
We guess that for some constant . Substituting this guess into recurrence (7.1) yields
Every term in the maximization is bounded by , when (see Exercise 7.4-3), as explained in the book. Continuing our analysis of , we obtain
by picking the constant small enough that the term dominates. Therefore, .
Exercise 7.4-2
Available in the latest revision of the IM.
Exercise 7.4-3
A quadratic polynomial of can have at most two distinct zeros. As explained in the book, implies that . When this expression is zero we have a maximum, over all values of , for . Apparently, the two distinct zeros are .
Exercise 7.4-4
This bound and Lemma 7.1 allow us to conclude that the expected running time of Randomized-Quicksort is .
Exercise 7.4-5
Insertion sort requires time to sort an array of size . The total time spent by insertion sort is , since we can have at most subarrays of size . The expected number of levels in the recursion tree is . Therefore, the total time spent in partitioning subarrays is . After summing up these terms, we get that the randomized version of this sorting algorithm runs in expected time.
The modified algorithm has the same asymptotic running time as standard randomized quick-sort when . The largest asymptotic value of , as a function of , that satisfies this condition is . Notice that cannot be asymptotically larger than , otherwise and we would make things worse. On the other hand, plugging in into our formula for the expected running time gives
In practice, you would need to experimentally find the best , as performance depends on many other machine related factors. Usually, you would want to select the largest possible among all values that give proper performance advantages.
Exercise 7.4-6
If the median falls into the lowest or largest numbers, then we get a worse than -to- split. This happens if at least two randomly selected numbers belong in these ranges. We have the following 4 disjoint events:
All three numbers are among the lowest values.
All three numbers are among the largest values.
Exactly two numbers are among the lowest values.
Exactly two numbers are among the largest values.
Therefore, the total probability is
Problem 7-1
a.
Initialization: A=(13, 19, 9, 5, 12, 8, 7, 4, 11, 2, 6, 21) p=1 r=12 x=13 i=0 j=13
After Iteration 1: A=(6, 19, 9, 5, 12, 8, 7, 4, 11, 2, 13, 21) i=1 j=11
After Iteration 2: A=(6, 2, 9, 5, 12, 8, 7, 4, 11, 19, 13, 21) i=2 j=10
After Iteration 3: A=(6, 2, 9, 5, 12, 8, 7, 4, 11, 19, 13, 21) i=10 j=9 RETURN 9
b.
Hoare-Partition produces a balanced split instead of hitting the worst-case scenario, as is the case with Partition. It better distributes repeated elements equal to pivot, so it works well even in presence of duplicates.
c.
We focus on the first iteration and show what happens to indices and as well as what transformation of ensures that none of these indices ever fall out of range. Initially, before entering the while loop, we have , and . The body of the loop runs as follows:
Index is decremented before reading , so on first access.
It will be decremented until stumbling across an element not greater than . Notice that cannot pass , hence we know that after line 7.
Index is incremented before reading , so on first access.
It will be incremented until stumbling across an element not smaller than . Notice that cannot pass , hence we know that after line 10.
At this point, we have two possibilities at line 11:
If , then the function returns . This occurs when all other elements are greater than .
Otherwise, we must have , so and are swapped. But exactly these values, now on opposite positions relative to indices and , guard and from passing them. Furthermore, if either or (or both) stop at these locations, then we would surely have and the function would return .
d.
Again, we focus our attention on the first iteration. From part (c) we know that at termination. Before accessing index is decremented, so . Now, based on the fact that , we have two scenarios:
If , then these values are exchanged, since at line 11 and . In the next iteration, will be decremented at least once.
Otherwise, is immediately decremented below .
At any rate, at termination we have that .
e.
Let's prove the next invariant:
At the beginning of the while loop at lines 4-13, , the subarray contains elements less than or equal to the pivot, and the subarray contains elements greater than or equal to the pivot.
Initialization
Before entering the loop for the first time, we have , and . The empty subarrays and satisfy the invariant and .
Maintenance
The repeat-until loop at lines 5-7 decreases up to some position . We know that every element of is greater than or equal to . So, has only elements greater than or equal to .
The repeat-until loop at lines 8-10 increases up to some position . We know that every element of is less than or equal to . So, has only elements less than or equal to .
Postcondition: Let and . After line 10, but before entering line 11, we have that the subarray contains elements less than or equal to the pivot, and the subarray contains elements greater than or equal to the pivot. Furthermore, and .
If at line 11, then and are exchanged, so that the subarray contains elements less than or equal to the pivot and the subarray contains elements greater than or equal to the pivot. Hence, the invariant is reestablished for the next iteration.
Termination
The while loop must eventually terminate, since both indices are moving closer to each other at every iteration. This happens when at line 11. Nonetheless, no elements are exchanged, so the postcondition stated in the previous section remains true. We can differentiate the following cases:
If , then , thus the subarray contains elements less than or equal to the pivot and the subarray contains elements greater than or equal to the pivot.
Otherwise, , so the above conclusion again follows.
In both cases, It is true the every element from is less than or equal to every element from .
f.
Quicksort(A, p, r)
if p < r
q = Hoare-Partition(A, p, r)
Quicksort(A, p, q)
Quicksort(A, q + 1, r)Problem 7-2
Available in the latest revision of the IM.
There is an omission in the IM for part (c). Randomized-Partition' also differs at line 2, where must be exchanged with instead of .
In part (d) the IM does not cover the best-case scenario. Namely, when all element values are equal, then Quicksort' runs in linear time.
Problem 7-3
Available in the latest revision of the IM.
Problem 7-4
Available in the latest revision of the IM.
Problem 7-5
Available in the latest revision of the IM.
Problem 7-6
a.
The three randomly selected elements must include itself, one smaller than , and one larger than . These elements can be chosen in any order, so we should count the number of 3-sets satisfying these constraints among such sets. We have .
b.
We need to find at the limit, when . Observe that tends to as increases. Therefore, we have
c.
For convenience, suppose is a multiple of 3. We need to find at the limit, when . Following the hint from the book, we approximate the sum by an integral.
Thus, the median-of-3 method increases the likelihood of getting a good split compared with the ordinary implementation by 13/9.
d.
The running time of quicksort is still valid even if the algorithm chooses the best pivot (median of the subarray) in every partitioning. Thus, any technique that improves the chances of having good splits cannot influence the asymptotic lower bound of quicksort.
Problem 7-7
a.
The Fuzzy-Partition function is similar to Partition' from Problem 7-2, part (b). The rest of the code of randomized quicksort uses this partitioning function instead of the ordinary version.
Fuzzy-Partition(A, p, r)
x = A[p] // Pivot is chosen to be at the beginning of A[p:r].
i = p
h = p
for j = p + 1 to r
if A[j].b < x.a
y = A[j]
A[j] = A[h + 1]
A[h + 1] = A[i]
A[i] = y
i = i + 1
h = h + 1
elseif A[j].a <= x.b
// Mutually overlapping intervals including the pivot are treated as equal.
// There could be many such joint sets of intervals, we select one of them.
x.a = Max(A[j].a, x.a)
x.b = Min(A[j].b, x.b)
exchange A[h + 1] with A[j]
h = h + 1
return i, h // A[i:h] elements fuzzy-equal to x.b.
Observe that as the intervals overlap more and more, the "equal" sector will grow. These intervals will be omitted from subsequent recursive calls. When all intervals overlap, then after calling Fuzzy-Partition once from the top level, the whole array becomes sorted. Therefore, the expected running time reduces to .
Otherwise, our algorithm runs in usual expected time.
Last updated