7-5 Solution PDF
7-5 Solution PDF
7-5 Solution PDF
James Vanderhyde
1. Problem 7-5.
(a) Let x1 , x2 , and x3 be the three values that are picked at random. Since there
are 3! = 6 ways to arrange 3 elements, we can calculate the probability assuming
1
ni
x1 < x2 < x3 and multiply by 6: pi = 6 Pr[x1 < i, x2 = i, x3 > i] = 6 n2 n1
n2
=
6(i1)(ni)
.
n(n1)(n2)
(b) Ill assume n is even, since it doesnt affect the ratio in the limit. pn/2 =
2 2n)
pn/2
6( n
1)( n
)
1
2
2
= 2(n3(n
3 3n2 +2n) . For the ordinary implementation, p = n . So limn
n(n1)(n2)
p
2
2n)
3(12/n)
3
= lim 2(n3n(n
3 3n2 +2n) = lim 2(13/n+2/n2 ) = 2 .
(c) For the new method, the probability of a good split is Pr[ n3 i 2n
] =
3
P2n/3 6(i1)(ni)
i=n/3 n(n1)(n2) . To approximate by an integral, Ill use the variable t to mean
the fraction of the way from 1 to n. Then n, n 1, n 2 are all nearly 1 (all of the
R 2/3
R 2/3
way through). So the approximating integral is 1/3 6t(1t)
dt
=
6(t t2 )dt =
111
1/3
2/3
6( 12 t2 31 t3 )|1/3 = 13
. For the old method, the probability of a good split is just
27
P
2n/3
] = i=n/3 n1 = n1 n3 = 13 < 13
.
Pr[ n3 i 2n
3
27
(d) The best possible running time for any version of quicksort would be achieved
when the median is picked for the pivot every time, but this is still only (n lg n)
since the array is divided in half every time and the recursion handles both halves.
Thus any scheme for picking the pivot, such as the median-of-three considered in
this problem, cannot affect the asymptotic running time, but only the constant
factor in the running time.
2. Parts ad of Problem 8-5.
(a) 1-sorted is equivalent to completely sorted, in the standard use of the term.
(b) Here are some possible examples: {2, 1, 3, 4, 5, 6, 7, 8, 9, 10} {2, 1, 4, 3, 6, 5, 8, 7, 10, 9}
{6, 1, 7, 2, 8, 3, 9, 4, 10, 5}.
P
P
(c) Note simply that Ai Ai+k i+k1
Aj i+k
j=i
j=i+1 Aj = Ai +Ai+1 +. . .+Ai+k2 +
P
Ai+k1 Ai+1 Ai+2 . . . Ai+k1 Ai+k = Ai Ai+k 0 k1 i+k1
Aj
j=i
Pi+k
1
j=i+1 Aj .
k
(d) Split the array (arbitrarily) into k subarrays of size b nk c or b nk c + 1 each. Sort
each subarray at a cost of O( nk lg nk ) each to form sorted subarrays A1 , . . . , Ak . So
far this is a total of O(k( nk lg nk )) = O(n lg nk ). Finally, interleave the the k sorted
subarrays to form one k-sorted array A: A[i] = Ai mod k [i div k]. This involves
O(n) operations but no comparisons. Thus the total time is O(n lg nk ). This works
because for i {1, . . . , n k}, A[i] = Ai mod k [i div k] < Ai mod k [(i div k) + 1] =
Assignment 2 Solutions
James Vanderhyde
mod k
3. Problem 9.1-1.
First find the minimum in the list in rounds in this way: Split the array into pairs and
compare each pair. The smaller in each comparison advances to the next round, which
has half as many elements to compare. At the end only the global minimum will be
left. This is n2 + n4 + n8 + . . . + 1 = n2 (2)(1 n1 ) = n 1 comparisons. Now, note that
the second smallest element could only have been eliminated by the global minimum.
Therefore, it must be one of the dlg ne elements that the global min was compared to.
So simply find the min among these elements and return it as the second smallest in
the whole array. The total is n 1 + dlg ne 1 comparisons.
4. Problem 9.3-1.
If the input elements are divided into groups of 7, the analysis will mostly be the same
as when they are divided
1 n intogroups of 5. The number of elements greater than x will
now be at least 4 2 d 7 e 2 , and so Select is called recursively on at most 5n/7+8
elements in step 5. Thus we have the recurrence T (n) T (dn/7e)+T (5n/7+8)+O(n),
and the substitution method gives us T (n) 6cn/7 + 9c + an cn for an appropriate
value of c and for large enough n. Thus groups of 7 will work.
However, this same analysis does not go through for groups of 3. In fact, one can show
that the running time for Select using groups of 3 is (n lg n)
therefore cannot be
1and
n
O(n): The number of elements greater than x is at most 2 2 b 3 c n3 , so Select
is called recursively on at least 2n
elements in step 5. We can use the substituion
3
) + (n) T ( n3 ) +
method to prove T (n) = (n lg n) like this: T (n) T (d n3 e) + T ( 2n
3
2n
n
n
2n
2n
T ( 3 ) + (n) c( 3 ) lg( 3 ) + c( 3 ) lg( 3 ) + an = cn lg n cn lg 3 + 23 cn + an cn lg n
a
if c lg 32/3
. This completes the argument.
5. Problem 9.3-7.
Use the O(n)-time Select algorithm to find the median m of S. I assume their
measure of closest means that the distance of a number x to m is |m x|. We create
a new array D, the same size as S, that stores the distance to m for each element in
S. Now we use the O(n)-time Select algorithm again but to find the kth smallest
element in D. Say this occurs at index j. Now run through D and keep track of all the
indices I1 that have value less than D[j] and all the indices I2 that have value equal to
D[j]. Finally, output all the elements of S that correspond to I1 and enough elements
that correspond to I2 to total k output values.
The other way to interpret this problem is that they wanted the k elements of S
centered at the median, in other words, k/2 elements on each side of m. We can solve
this problem as follows: Use the O(n)-time Select algorithm to find order statistics
Assignment 2 Solutions
James Vanderhyde
n/2 k/2 and n/2 + k/2. Then run through the list S and output all the values that
are between the two found values.
6. Problem 9.3-8.
To get an O(lg n)-time algorithm for this search problem, we will need to eliminate
half the input at each step. Note that finding the median in a sorted array A of length
n is O(1), since the median is just A[n/2]. So let x1 = X[n/2] and y1 = Y [n/2], and
let m be the median value were looking for. There are four possible locations for m:
X[1, . . . , n2 ], X[ n2 , . . . , n], Y [1, . . . , n2 ], and Y [ n2 , . . . , n]. There are 2n/2 = n values less
than m in X and Y , but only (say) k of them in X and n k of them in Y . Suppose
first that x1 < y1 . If m < x1 , then k < n2 . But that would mean the median is greater
than Y [1], . . . , Y [n k], including Y [ n2 ] = y1 > x1 , which contradicts that m < x1 .
Therefore we can eliminate X[1, . . . , n2 ] for the median search. Likewise, m > y1 leads
to a contradiction, eliminating Y [ n2 , . . . , n]. Thus we have eliminated half the input,
and we recurse on the two subarrays X[ n2 , . . . , n] and Y [1, . . . , n2 ]. If x1 > y1 , we
recurse on X[1, . . . , n2 ] and Y [ n2 , . . . , n] instead. The recursion stops when n = 1, and
by convention (see CLRS p. 190) we return min{X[1], Y [1]}.