Download presentation
Presentation is loading. Please wait.
Published byJustina Morrison Modified over 9 years ago
1
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
2
20071026 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.
3
20071026 chap07Hsiu-Hui Lee3 Initial call: Q UICKSORT (A, 1, n)
4
20071026 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 + 1 6 exchange A[i] A[j] 7 exchange A[i+1] A[r] 8 return i+1
5
20071026 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.
6
20071026 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 + 1 6 exchange A[i] A[j] 7 exchange A[i+1] A[r] 8 return i+1
7
20071026 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 + 1 6 exchange A[i] A[j] 7 exchange A[i+1] A[r] 8 return i+1 Why??? (Exercise 7.1-3)
8
20071026 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 + 1 6 exchange A[i] A[j] 7 exchange A[i] A[p] 8 return i Select first element as pivot
9
20071026 chap07Hsiu-Hui Lee9 Another partitioning example 6 10 13 5 8 32 11 i j
10
20071026 chap07Hsiu-Hui Lee10
11
20071026 chap07Hsiu-Hui Lee11
12
20071026 chap07Hsiu-Hui Lee12
13
20071026 chap07Hsiu-Hui Lee13
14
20071026 chap07Hsiu-Hui Lee14
15
20071026 chap07Hsiu-Hui Lee15
16
20071026 chap07Hsiu-Hui Lee16
17
20071026 chap07Hsiu-Hui Lee17
18
20071026 chap07Hsiu-Hui Lee18
19
20071026 chap07Hsiu-Hui Lee19
20
20071026 chap07Hsiu-Hui Lee20
21
20071026 chap07Hsiu-Hui Lee21 7.2 Performance of quicksort Why??? (Exercise 7.2-1) Why??? The input array is already completely sorted. No better than insertion sort.
22
20071026 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).
23
20071026 chap07Hsiu-Hui Lee23
24
20071026 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
25
20071026 chap07Hsiu-Hui Lee25 7.3 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.
26
20071026 chap07Hsiu-Hui Lee26
27
20071026 chap07Hsiu-Hui Lee27 7.4 Analysis of quicksort
28
20071026 chap07Hsiu-Hui Lee28 7.4.2 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)
29
20071026 chap07Hsiu-Hui Lee29 we define {z i is compared to z j }, {z i is compared to z j }
30
20071026 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 }
31
20071026 chap07Hsiu-Hui Lee31 by pp. 1060 A.7
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.