Quick-Sort 4/9/2002 10:1
Outline and Reading
Quick-sort (§4.3)
Quick-Sort Algorithm
Partition step
7 4 9 6 2 → 2 4 6 7 9 Quick-sort tree
Execution example
4 2 → 2 4 7 9 → 7 9
Analysis of quick-sort (4.3.1)
2→2 9→9 In-place quick-sort (§4.8)
Summary of sorting algorithms
Quick-Sort 1 Quick-Sort 2
Quick-Sort Partition
Quick-sort is a randomized We partition an input Algorithm partition(S, p)
sequence as follows: Input sequence S, position p of pivot
sorting algorithm based x We remove, in turn, each Output subsequences L, E, G of the
on the divide-and-conquer element y from S and elements of S less than, equal to,
or greater than the pivot, resp.
paradigm: We insert y into L, E or G,
L, E, G ← empty sequences
Divide: pick a random depending on the result of
x ← S.remove(p)
element x (called pivot) and the comparison with the
x pivot x while ¬S.isEmpty()
partition S into y ← S.remove(S.first())
L elements less than x
Each insertion and removal
if y < x
is at the beginning or at the
E elements equal x L E G L.insertLast(y)
end of a sequence, and
G elements greater than x else if y = x
hence takes O(1) time
Recur: sort L and G E.insertLast(y)
Thus, the partition step of else { y > x }
Conquer: join L, E and G x quick-sort takes O(n) time G.insertLast(y)
return L, E, G
Quick-Sort 3 Quick-Sort 4
Quick-Sort Tree Execution Example
An execution of quick-sort is depicted by a binary tree
Each node represents a recursive call of quick-sort and stores
Pivot selection
Unsorted sequence before the execution and its pivot
Sorted sequence at the end of the execution 7 2 9 43 7 6 1 → 1 2 3 4 6 7 8 9
The root is the initial call
The leaves are calls on subsequences of size 0 or 1
7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6
7 4 9 6 2 → 2 4 6 7 9
2→2 9 4 → 4 9 3→3 8→8
4 2 → 2 4 7 9 → 7 9
9→9 4→4
2→2 9→9
Quick-Sort 5 Quick-Sort 6
Quick-Sort 4/9/2002 10:1
Execution Example (cont.) Execution Example (cont.)
Partition, recursive call, pivot selection Partition, recursive call, base case
7 2 9 4 3 7 6 1→ 1 2 3 4 6 7 8 9 7 2 9 43 7 6 1→→ 1 2 3 4 6 7 8 9
2 4 3 1→ 2 4 7 9 3 8 6 1 → 1 3 8 6 2 4 3 1 →→ 2 4 7 3 8 6 1 → 1 3 8 6
2→2 9 4 → 4 9 3→3 8→8 1→1 9 4 → 4 9 3→3 8→8
9→9 4→4 9→9 4→4
Quick-Sort 7 Quick-Sort 8
Execution Example (cont.) Execution Example (cont.)
Recursive call, …, base case, join Recursive call, pivot selection
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9 7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1 → 1 2 3 4 3 8 6 1 → 1 3 8 6 2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6
1→1 4 3 → 3 4 3→3 8→8 1→1 4 3 → 3 4 8→8 9→9
9→9 4→4 9→9 4→4
Quick-Sort 9 Quick-Sort 10
Execution Example (cont.) Execution Example (cont.)
Partition, …, recursive call, base case Join, join
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9 7 2 9 4 3 7 6 1 →1 2 3 4 6 7 7 9
2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6 2 4 3 1 → 1 2 3 4 7 9 7 → 17 7 9
1→1 4 3 → 3 4 8→8 9→9 1→1 4 3 → 3 4 8→8 9→9
9→9 4→4 9→9 4→4
Quick-Sort 11 Quick-Sort 12
Quick-Sort 4/9/2002 10:1
Worst-case Running Time Expected Running Time
The worst case for quick-sort occurs when the pivot is the unique Consider a recursive call of quick-sort on a sequence of size s
minimum or maximum element Good call: the sizes of L and G are each less than 3s/4
One of L and G has size n − 1 and the other has size 0 Bad call: one of L and G has size greater than 3s/4
The running time is proportional to the sum 7 2 9 43 7 6 19 7 2 9 43 7 6 1
n + (n − 1) + … + 2 + 1
2 4 3 1 7 9 7 1 → 1 1 7294376
Thus, the worst-case running time of quick-sort is O(n2)
depth time Good call Bad call
0 n A call is good with probability 1/2
1/2 of the possible pivots cause good calls:
1 n−1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
…
… …
Bad pivots Good pivots Bad pivots
n−1 1
Quick-Sort 13 Quick-Sort 14
Expected Running Time, Part 2 In-Place Quick-Sort
Probabilistic Fact: The expected number of coin tosses required in Quick-sort can be implemented
order to get k heads is 2k to run in-place
For a node of depth i, we expect In the partition step, we use Algorithm inPlaceQuickSort(S, l, r)
i/2 ancestors are good calls replace operations to rearrange Input sequence S, ranks l and r
The size of the input sequence for the current call is at most (3/4)i/2n the elements of the input Output sequence S with the
sequence such that elements of rank between l and r
Therefore, we have expected height time per level
rearranged in increasing order
s(r) O(n) the elements less than the
For a node of depth 2log4/3n, if l ≥ r
the expected input size is one pivot have rank less than h
the elements equal to the pivot
return
The expected height of the s(a) s(b) O(n)
quick-sort tree is O(log n) have rank between h and k i ← a random integer between l and r
O(log n) the elements greater than the x ← S.elemAtRank(i)
The amount or work done at the s(c) s(d) s(e) s(f) O(n)
pivot have rank greater than k
nodes of the same depth is O(n) (h, k) ← inPlacePartition(x)
The recursive calls consider inPlaceQuickSort(S, l, h − 1)
Thus, the expected running time
elements with rank less than h inPlaceQuickSort(S, k + 1, r)
of quick-sort is O(n log n)
elements with rank greater
total expected time: O(n log n) than k
Quick-Sort 15 Quick-Sort 16
Summary of Sorting Algorithms
Algorithm Time Notes
in-place
selection-sort O(n2) slow (good for small inputs)
in-place
insertion-sort O(n2) slow (good for small inputs)
O(n log n) in-place, randomized
quick-sort
expected fastest (good for large inputs)
in-place
heap-sort O(n log n) fast (good for large inputs)
sequential data access
merge-sort O(n log n) fast (good for huge inputs)
Quick-Sort 17