4.1. Quick Sort
4.1. Quick Sort
i pj
(a) 28713564
(b) 28713 564
(c) 2 7 ||356 4
(d) 2 1 3 5 64
pi
(e) 2 1 356 4
() 2 13 64|
(g) |2 13 64
p
(h) 2| 1|3
p
(i) 2 13 4
Figure 7.1 The operation of PARTITION on a sample array. Lightly shaded array elements are all in
the first partition with values no greater thanx. Heavily shaded elements are in the second partition
with values greater than x. The unshaded elements have not yet been put in one of the first two
partitions, and the final white element is the pivot. (a) The initial array and variable settings. None of
the elements have been placed in either of the first two partitions. (b) The value 2 is "swapped with
partition
itself" and put in the partition of smaller values. (c)<d) The values 8and 7are added to the
grows. () The values
of larger values. (e) The values 1 and 8 are swapped, and the smaller partition include
grows to
3and 8 are swapped, and the smaller partition grows. (g-(h) The larger partition
lines 7-8, the pivot element is swapped so that it lies between
5and 6 and the loop terminates. (i) In
the two partitions.
Initialization: Prior to the first iteration of the loop, i p-1, and j = p. There
i + 1 and j - l, so the
are no values between p and i, and no values between
The assignment
first two conditions of the loop invariant are trivially satisfied.
condition.
in line 1l satisfies the third
consider, depending
Maintenance: As Figure 7.3shows, there are two cases to what happens when
7.5(a) shows
on the outcome of the test in line 4. igure
148 Chapter 7 Quicksort
unrestricted
Figure 7.2 The four regions maintained by the procedure PARTITION on a subarray A[p
values in A[p..i) are all lessthan or equal to x, the values in A[i +1..j - 1] are all greater. r).than
Thex,
and A[r] =x. The values in Aj..r - 1] can take on any values.
AU] > x; the only action in the loop is to increment j. After i is incr
mented, condition 2 holds for A[j 1]and all other entries remain unchanged
Figure 7.3(b) shows what happens when A[j] s x; iis incremented, Ail
and ALj] are swapped, and then j is incremented. Because of the Swap, we
now have that A[i]<x, and condition lis satisfied. Similarly, we also have
that A[j- 1] > x, since the item that was swapped into A[j - 1] is, by the
loop invariant, greater than x.
Termination: At termination,j=r. Therefore, every entry in the array is in one
of the three sets described by the invariant, and we have partitioned the values
in the aray into three sets: those less than or equal to x, those greater than x,
and a singleton set containing x.
The final two lines of PARTITION Move the pivot element into its place in the
middle of the array by swapping it with the leftmost element that is greater than X.
The output of PARTITION now satisfies the specifications given for the divide step.
The running time of PARTITION on the subarray A[p..r] is (n), wher
n=r-p+1(see Exercise 7.1-3).
Exercises
7.1-1
Using Figure 7.1 as a model, illustrate the operation of PARTITION onthe array
A= (13, 19,9, 5, 12, 8,7, 4, 11, 2,
6,21).
7.1-2
What value of g does Alp..l
PARTITION return when all elements in the arTay
have the same value? Modify (p+r)/2 when.all elements
in the array A[p..r]have the PARTITION that
SO q=
same value.
7.1-3 n
ofsize
Give a brief argument that the time of PARTITION on asubarray
is (n). running
7.2 Performance of quicksort 149
(a)
>
(b)
>X
Figure 7.3 The two cases for one iteration of procedure PARTITION. (a) If Aj] > x, the oniy
action is to increment i, which maintains the loop invariant. (b) If Ai] <x, index i is incremented,
A[i] and AU]are swapped, and then j is incremented. Again, the loop invariant is maintained.
7.1-4
How would you modify QUICKSORT to sort into nonincreasing order?
Worst-case partitioning
routine nro.
The worst-case behavior for quicksort occurs when the partitioning
Oelements. (This clains
duces one subproblem withn - lelements and one with partitioning arises
is proved in Section 7.4.1.) Let us asSume that this unbalanced
time. Since the recursive l
in each recursive call. The partitioning costs (n)
150
Chapter 7 Qicksort
on an array of size 0just returns, T(0) = (1), and the recurrence for
time is the running
T(n) = T(n - 1) + T(0) + (n)
= T(n - 1) + O(n)
Intuitively, if we sum the costs incurred at each level of the recursion,
an arithmetic series (equation (A.2), which evaluates to On2).
Indeed,
straightforward to use the substitution method to prove that the recurrence
T(n 1) + O(n) has the solution T(n) = O(n').(See Exercise 7.2-1.) T(n)=
Thus, if the partitioning is maximally unbalanced at every recursive level of tha
algorithm, the running time is 0(n'). Therefore the worst-case running time of
quicksort is no better than that of insertion sort. Moreover, the O(n') running time
occurs when the input array is already completely sorted-a common situation in
which insertion sort runs in O
(n) time.
Best-case partitioning
In the most even possible split, PARTITION produces two subproblems, each of
size no more than n/2, since one is of size n/2] and one of size [n/2]-1. In this
case, quicksort runs much faster. The recurrence for the running time is then
T(n) < 2T (n/2) + (n),
which by case 2 of the master theorem (Theorem 4.1) has the solution T(n) =
O(nlgn). Thus, the equal balancing of the two sides of the partition at every level
of the recursion produces an asymptotically faster algorithm.
Balanced partitioning
The average-case running time of quicksort is much closerto the best case thanto
the worst case, as the analyses in Section 7.4 will show. The key to understand
ing why is to understand how the balance of the partitioningis reflectedinthe
recurrence that describes the running time. 9-(0-!
a
Suppose, for example, that the partitioning algorithm always producesobtain
the
proportional split, which at first blush seems quite unbalanced. We then
recurTence
T(n) <T (9n/10) +
T(n/10) + cn constant
on the running time of the
where we have explicitly included
hidden in the O(n) term.quicksort,
recurrence
recursion tree for this
Figure 7.4 shows the
condition isreached
Notice that every level of the tree has cost cn, until a boundary
at depth logo = quicksort
(lg n), and then the levels have cost at mostcsot i
cursion terminates at depth log10/9 n= (lg n). The total cost of