Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
chap07Hsiu-Hui Lee2 7.1 Description of quicksort Quicksort an n-element array: Divide: Partition the array into two subarrays around a pivot x such that elements in lower subarray ≤ x ≤ elements in upper subarray. ≤ x x ≥ x 2.Conquer: Recursively sort the two subarrays. 3. Combine: Trivial. Key: Linear-time partitioning subroutine.
chap07Hsiu-Hui Lee3 Initial call: Q UICKSORT (A, 1, n)
chap07Hsiu-Hui Lee4 Partition(A, p, r) 1 x A[r] 2 i p – 1 3 for j p to r -1 4 do if A[j] ≤ x 5 then i i exchange A[i] A[j] 7 exchange A[i+1] A[r] 8 return i+1
chap07Hsiu-Hui Lee5 Loop Invariant 1. All entries in A[p..i] are ≤ pivot. 2. All entries in A[i+1..j-1] are > pivot. 3. A[r] = pivot.
chap07Hsiu-Hui Lee6 The operation of Partition on a sample array 1 x A[r] 2 i p – 1 3 for j p to r -1 4 do if A[j] ≤ x 5 then i i exchange A[i] A[j] 7 exchange A[i+1] A[r] 8 return i+1
chap07Hsiu-Hui Lee7 Two cases for one iteration of procedure Partition Complexity: Partition on A[p…r] is (n) where n = r –p +1 1 x A[r] 2 i p – 1 3 for j p to r -1 4 do if A[j] ≤ x 5 then i i exchange A[i] A[j] 7 exchange A[i+1] A[r] 8 return i+1 Why??? (Exercise 7.1-3)
chap07Hsiu-Hui Lee8 Another Partitioning subroutine Invariant: x x x ? p ij r Partition(A, p, r) 1 x A[p] 2 i p 3 for j p+1 to r 4 do if A[j] ≤ x 5 then i i exchange A[i] A[j] 7 exchange A[i] A[p] 8 return i Select first element as pivot
chap07Hsiu-Hui Lee9 Another partitioning example i j
chap07Hsiu-Hui Lee10
chap07Hsiu-Hui Lee11
chap07Hsiu-Hui Lee12
chap07Hsiu-Hui Lee13
chap07Hsiu-Hui Lee14
chap07Hsiu-Hui Lee15
chap07Hsiu-Hui Lee16
chap07Hsiu-Hui Lee17
chap07Hsiu-Hui Lee18
chap07Hsiu-Hui Lee19
chap07Hsiu-Hui Lee20
chap07Hsiu-Hui Lee Performance of quicksort Why??? (Exercise 7.2-1) Why??? The input array is already completely sorted. No better than insertion sort.
chap07Hsiu-Hui Lee22 Quicksort ’ s average running time is much closer to the best case than to the worst case. Imagine that PARTITION always produces 9-to-1 split. Any split of constant proportionality will yield a recursion tree of depth, where the cost at each level is O(n).
chap07Hsiu-Hui Lee23
chap07Hsiu-Hui Lee24 Intuition for the average case T(n) = (n lg n) Splits will not always be constant. Mix of good and bad splits throughout the recursion tree
chap07Hsiu-Hui Lee Randomized quicksort We have assumed that all input permutations are equally likely. (not always true) To correct this, add randomization to randomly permute the input array Instead, we use random sampling picking one element at random. Don ’ t always use A[r] as the pivot, and randomly pick an element from the subarray that is being sorted.
chap07Hsiu-Hui Lee26
chap07Hsiu-Hui Lee Analysis of quicksort
chap07Hsiu-Hui Lee Expected running time Running time and comparisons Lemma 7.1 Let X be the number of comparisons performed in line 4 of partition over the entire execution of Quicksort on an n- element array. Then the running rime of Quicksort is O(n+X)
chap07Hsiu-Hui Lee29 we define {z i is compared to z j }, {z i is compared to z j }
chap07Hsiu-Hui Lee30 Pr{z i is compared to z j } = Pr{z i or z j is first pivot chosen from Z ij } = Pr{z i is first pivot chosen from Z ij } + Pr{z j is first pivot chosen from Z ij }
chap07Hsiu-Hui Lee31 by pp A.7