07-quicksort
07-quicksort
Quicksort
15-122: Principles of Imperative Computation (Summer 1 2015)
Frank Pfenning
1 Introduction
In this lecture we consider two related algorithms for sorting that achieve a
much better running time than the selection sort from last lecture: merge-
sort and quicksort. We developed quicksort and its invariants in detail.
As usual, contracts and loop invariants will bridge the gap between the
abstract idea of the algorithm and its implementation.
We will revisit many of the computational thinking, algorithm, and pro-
gramming concepts from the previous lectures. We highlight the following
important ones:
L ECTURE N OTES
Quicksort L7.2
this, quicksort not quite, which presents an interesting tradeoff when con-
sidering which algorithm to chose for a particular class of applications.
Recall linear search for an element in an array, which has asymptotic
complexity of O(n). The divide-and-conquer technique of binary search
divides the array in half, determines which half our element would have
to be in, and then proceeds with only that subarray. An interesting twist
here is that we divide, but then we need to conquer only a single new sub-
problem. So if the length of the array is 2k and we divide it by two on each
step, we need at most k iterations. Since there is only a constant number of
operations on each iteration, the overall complexity is O(log(n)). As a side
remark, if we divided the array into 3 equal sections, the complexity would
remain O(log(n)) because 3k = (2log2 (3) )k = 2log2 3∗k , so log2 (n) and log3 (n)
only differ in a constant factor, namely log2 (3).
2. Divide the array into two segments, those that are smaller and those
that are greater, with the pivot in between (the partition phase).
3. Recursively sort the segments to the left and right of the pivot.
3, 1, 4, 4, 7, 2, 8
and we pick 3 as our pivot. Then we have to compare each element of this
(unsorted!) array to the pivot to obtain a partition where 2, 1 are to the left
and 4, 7, 8, 4 are to the right of the pivot. We have picked an arbitrary order
for the elements in the array segments: all that matters is that all smaller
ones are to the left of the pivot and all larger ones are to the right.
Since we have to compare each element to the pivot, but otherwise
just collect the elements, it seems that the partition phase of the algorithm
L ECTURE N OTES
Quicksort L7.3
should have complexity O(k), where k is the length of the array segment
we have to partition.
It should be clear that in the ideal (best) case, the pivot element will be
magically the median value among the array values. This just means that
half the values will end up in the left partition and half the values will end
up in the right partition. So we go from the problem of sorting an array of
length n to an array of length n/2. Repeating this process, we obtain the
following picture:
n
1
par**on
*
n:
O(n)
n/2
n/2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
At each level the total work is O(n) operations to perform the partition.
In the best case there will be O(log(n)) levels, leading us to the O(nlog(n))
best-case asymptotic complexity.
How many recursive calls do we have in the worst case, and how long
are the array segments? In the worst case, we always pick either the small-
est or largest element in the array so that one side of the partition will be
empty, and the other has all elements except for the pivot itself. In the ex-
ample above, the recursive calls might proceed as follows (where we have
L ECTURE N OTES
Quicksort L7.4
array pivot
[3, 1, 4, 4, 8, 2, 7] 1
1, [3, 4, 4, 8, 2, 7] 2
1, 2, [3, 4, 4, 8, 7] 3
1, 2, 3, [4, 4, 8, 8] 4
1, 2, 3, 4, [4, 8, 7] 4
1, 2, 3, 4, 4, [8, 7] 7
1, 2, 3, 4, 4, 7, [8]
All other recursive calls are with the empty array segment, since we never
have any unsorted elements less than the pivot. We see that in the worst
case there are n − 1 significant recursive calls for an array of size n. The
kth recursive call has to sort a subarray of size n − k, which proceeds by
partitioning, requiring O(n − k) comparisons.
This means that, overall, for some constant c we have
n−1
X n(n − 1)
c k=c ∈ O(n2 )
2
k=0
comparisons. Here we used the fact that O(p(n)) for a polynomial p(n) is
always equal to the O(nk ) where k is the leading exponent of the polyno-
mial. This is because the largest exponent of a polynomial will eventually
dominate the function, and big-O notation ignores constant coefficients.
So quicksort has quadratic complexity in the worst case. How can we
mitigate this? If we could always pick the median among the elements in
the subarray we are trying to sort, then half the elements would be less and
half the elements would be greater. So in this case there would be only
log(n) recursive calls, where at each layer we have to do a total amount of
n comparisons, yielding an asymptotic complexity of O(nlog(n)).
Unfortunately, it is not so easy to compute the median to obtain the
optimal partitioning. It is possible to compute the median of n elements in
O(n) time, but quicksort is rarely if ever implemented this way in practice.
One reason for this is that it turns out that if we pick a random element, the
algorithm will run in O(nlog(n)) time most of the time.
Randomness is very important if we want to claim the algorithm is
likely to run in O(nlog(n)) time. With any fixed-pick strategy, there will
be simple inputs on which the algorithm takes O(n2 ) steps. For example,
if we always pick the first element, then if we supply an array that is al-
ready sorted, quicksort will take O(n2 ) steps (and similarly if it is “almost”
L ECTURE N OTES
Quicksort L7.5
sorted with a few exceptions)! If we pick the pivot randomly each time, the
kind of array we get does not matter: the expected running time is always
the same, namely O(nlog(n)). It’s still possible that we could randomly pick
bad pivots, but probabilstically, the chance of this is very, very low. Prov-
ing this, however, is a different matter and beyond the scope of this course.
This is an important example on how to exploit randomness to obtain a re-
liable average case behavior, no matter what the distribution of the input
values.
L ECTURE N OTES
Quicksort L7.6
L ECTURE N OTES
Quicksort L7.7
Here we use the auxiliary functions ge_seg (for greater or equal than segment)
and le_seg (for less or equal than segment), where
L ECTURE N OTES
Quicksort L7.8
4 Partitioning
The trickiest aspect of quicksort is the partitioning step, in particular since
we want to perform this operation in place. Let’s consider the situation
when partition is called:
pivot_index
… 2 14 25 21 12 78 97 16 89 21 …
lo hi
Perhaps the first thing we notice is that we do not know where the pivot
will end up in the partitioned array! That’s because we don’t know how
many elements in the segment are smaller and how many are larger than
the pivot. In particular, the return value of partition could be different than
L ECTURE N OTES
Quicksort L7.9
the pivot index that we pass in, even if the value that used to be at the
pivot index in the array before calling partition will be at the returned in-
dex when partition is done.1 One idea is to make a pass over the segment
and count the number of smaller elements, move the pivot into its place,
and then scan the remaining elements and put them into their place. For-
tunately, this extra pass is not necessary. We start by moving the pivot
element out of the way, by swapping it with the leftmost element in the
array segment.
pivot
=
16
… 16 14 25 21 12 78 97 2 89 21 …
lo hi
Now the idea is to gradually work towards the middle, accumulating ele-
ments less than the pivot on the left and elements greater than the pivot on
the right end of the segment (excluding the pivot itself). For this purpose
we introduce two indices, left and right. We start them out as lower + 1 (to
avoid the stashed-away pivot) and upper .
pivot
=
16
… 16 14 25 21 12 78 97 2 89 21 …
Since 14 < pivot, we can advance the left index: this element is in the
proper place.
pivot
=
16
≤
pivot
… 16 14 25 21 12 78 97 2 89 21 …
L ECTURE N OTES
Quicksort L7.10
At this point, 25 > pivot, it needs to go on the right side of the array. If we
put it on the extreme right end of the array, we can then say that it is in it’s
proper place. We swap it into A[right − 1 ] and decrement the right index.
pivot
=
16
≤
pivot
≥
pivot
… 16 14 21 21 12 78 97 2 89 25 …
In the next two steps, we proceed by making swaps. First, we decide that
the 21 that is currently at left can be properly placed to the left of the 25, so
we swap it with the element to the left of 25. Then, we have 89 at A[left],
and so we can decide this is well-placed to the left of that 21.
pivot
=
16
≤
pivot
≥
pivot
… 16 14 89 21 12 78 97 2 21 25 …
pivot
=
16
≤
pivot
≥
pivot
… 16 14 2 21 12 78 97 89 21 25 …
Let’s take one more step: 2 < pivot, so we again just decide that the 2 is fine
where it is and increment left.
pivot
=
16
≤
pivot
≥
pivot
… 16 14 2 21 12 78 97 89 21 25 …
L ECTURE N OTES
Quicksort L7.11
At this point we pause to read off the general invariants which will
allow us to synthesize the program. We see:
(1) pivot ≥ A[lower + 1 ..left)
pivot
=
16
≤
pivot
≥
pivot
… 16 14 2 12 78 97 21 89 21 25 …
lo hi
Where do left and right need to be, according to our invariants? By in-
variant (1), all elements up to but excluding left must be less or equal to
pivot. To guarantee we are finished, therefore, the left must address the el-
ement 78 at lower + 4. Similarly, invariant (2) states that the pivot must be
less or equal to all elements starting from right up to but excluding upper .
Therefore, right must also address the element 3 at lower + 3.
pivot
=
16
≤
pivot
≥
pivot
… 16 14 2 12 78 97 21 89 21 25 …
This means after the last iteration, just before we exit the loop, we have
left = right, and throughout:
(4) lower + 1 ≤ left ≤ right ≤ upper
Now comes the last step: since left = right, pivot ≥ A[left − 1] and we
can swap the pivot at lower with the element at left − 1 to complete the
partition operation. We can also see the left − 1 should be returned as the
new position of the pivot element.
L ECTURE N OTES
Quicksort L7.12
5 Implementing Partitioning
Now that we understand the algorithm and its correctness proof, it remains
to turn these insights into code. We start by swapping the pivot element to
the beginning of the segment.
...
}
At this point we initialize left and right to lower + 1 and upper , respectively.
We have to make sure that the invariants are satisfied when we enter the
loop for the first time, so let’s write these.
The crucial observation here is that lower < upper by the precondition of
the function. Therefore left ≤ upper = right when we first enter the loop.
The segments A[lower + 1 ..left) and A[right..upper ) will both be empty, ini-
tially.
L ECTURE N OTES
Quicksort L7.13
The code in the body of the loop just compares the element at index left
with the pivot and either increments left, or swaps the element to A[right].
Now we just note the observations about the final loop state with an as-
sertion, swap the pivot into place, and return the index left. The complete
function is on the next page, for reference.
L ECTURE N OTES
Quicksort L7.14
6 Stability
Outside of introductory programming courses, much of the time, we don’t
sort arrays of integers but arrays of records, where the data we are trying to
sort (like a lecture number) is associated with another piece of information
(like a username). This is also what happens when we tell a spreadsheet to
L ECTURE N OTES
Quicksort L7.15
The resulting array is sorted by lecture number, but whereas lovelace ap-
peared after church in the unsorted array, they appear in the other order in
the sorted array. To put it another way, the first array is sorted by student
IDs, we might expect the Andrew IDs within Lecture 1 and Lecture 2 to be
sorted, but that’s not the case.
If a sort fulfills this expectation – if the relative order in which distinct
elements that the sort sees as equivalent, like (2, church) and (2, church),
is preserved by sorting – then the sort is said to be a stable sort.
Selection sort was in-place, stable, but slow. Quicksort was in-place, hope-
fully fast (with lucky or random pivot selection), but not stable. We’ll close
out or discussion of sorting with the presentation of a sort that’s stable, fast,
but not in-place.
7 Mergesort
Let’s think more generally about how to apply the divide-and-conquer
technique to sorting. How do we divide? In quicksort, the partition func-
tion is what divides the problem into two sub-problems, and it’s the first
thing we do. A characteristic of mergesort is that the divide phase of divide-
and-conquer is immediate: we only need to calculate the midpoint. On the
L ECTURE N OTES
Quicksort L7.16
We would still have to write merge, of course, but compare this to the quick-
sort implementation. They are very similar, but instead of a partition fol-
lowed by two recursive calls, we have two recursive calls followed by a
merge. We use the specification function is_sorted from the last lecture
that takes an array segment, defined by its lower and upper bounds.
The simple and efficient way to merge two sorted array segments (so
that the result is again sorted) is to create a temporary array, scan each of
the segments from left to right, copying the smaller of the two into the
temporary array. This is a linear time (O(n)) operation, but it also requires
a linear amount of temporary space. The merge operation can be seen in
the mergesort.c0 included as a part of this lecture’s code directory.
Let’s consider the asymptotic complexity of mergesort, assuming that
the merging operation is O(n).
L ECTURE N OTES
Quicksort L7.17
n
1
merge
*
n:
O(n)
n/2
n/2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
We see that the asymptotic running time will be O(nlog(n)), because there
are O(log(n)) levels, and on each level we have to perform O(n) operations
to merge. The midpoint calculation is deterministic, so this is a worst-case
bound.
L ECTURE N OTES
Quicksort L7.18
Exercises
Exercise 1 In this exercise we explore strengthening the contracts on in-place
sorting functions.
3. Discuss any specific difficulties or problems that arise. Assess the outcome.
Exercise 2 Prove that the precondition for sort together with the contract for
partition implies the postcondition. During this reasoning you may also assume
that the contract holds for recursive calls.
Exercise 3 Our implementation of partitioning did not pick a random pivot, but
took the middle element. Construct an array with seven elements on which our
algorithm will exhibit its worst-case behavior, that is, on each step, one of the par-
titions is empty.
L ECTURE N OTES