Lecture 10 - Quick Sort
Lecture 10 - Quick Sort
1
Recap
• Master Method
• Sorting Algorithms
• Selection Sort
• Bubble Sort
• Insertion Sort 2
Types of Sorting Algorithms
• Non-recursive/Incremental comparison sorting
• Selection sort
• Bubble sort
• Insertion sort
• Recursive comparison sorting
• Quick sort
• Merge sort
• Heap sort
• Non-comparison linear sorting
• Count sort
• Radix sort
• Bucket sort 3
Recursive Comparison Sorting
There are many ways to design algorithms:
Pivot
6
Quick Sort
Pivot
7
Quick Sort
Pivot
≤ ¿
8
Quick Sort
Pivot
≤ ¿
9
Quick Sort
Pivot
≤ ¿
≤ ¿ ≤ ¿
10
Quick Sort
11
Quick Sort Example
9 6 5 0 8 2 4 7
12
Quick Sort Example
𝐴 9 6 5 0 8 2 4 7
𝑝 𝑟
13
Quick Sort Example
𝑝𝑖𝑣𝑜𝑡
𝐴 9 6 5 0 8 2 4 7
𝑝 𝑟
14
Quick Sort Example
𝑖 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 9 6 5 0 8 2 4 7
𝑝 𝑟
15
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 9 6 5 0 8 2 4 7
𝑝 𝑟
16
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 9 6 5 0 8 2 4 7
𝑝 𝑟
17
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 9 6 5 0 8 2 4 7
𝑝 𝑟
18
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 9 6 5 0 8 2 4 7
𝑝 𝑟
19
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 9 6 5 0 8 2 4 7
𝑝 𝑟
20
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 9 5 0 8 2 4 7
𝑝 𝑟
21
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 9 5 0 8 2 4 7
𝑝 𝑟
22
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 9 5 0 8 2 4 7
𝑝 𝑟
23
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 9 5 0 8 2 4 7
𝑝 𝑟
24
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 9 0 8 2 4 7
𝑝 𝑟
25
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 9 0 8 2 4 7
𝑝 𝑟
26
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 9 0 8 2 4 7
𝑝 𝑟
27
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 9 0 8 2 4 7
𝑝 𝑟
28
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 9 8 2 4 7
𝑝 𝑟
29
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 9 8 2 4 7
𝑝 𝑟
30
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 9 8 2 4 7
𝑝 𝑟
31
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 9 8 2 4 7
𝑝 𝑟
32
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 9 8 2 4 7
𝑝 𝑟
33
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 9 8 2 4 7
𝑝 𝑟
34
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 9 8 2 4 7
𝑝 𝑟
35
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 2 8 9 4 7
𝑝 𝑟
36
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 2 8 9 4 7
𝑝 𝑟
37
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 2 8 9 4 7
𝑝 𝑟
38
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 2 8 9 4 7
𝑝 𝑟
39
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 2 4 9 8 7
𝑝 𝑟
40
Quick Sort Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝐴 6 5 0 2 4 9 8 7
𝑝 𝑟
41
Quick Sort Example 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝑖 +1 𝑗
𝐴 6 5 0 2 4 9 8 7
𝑝 𝑟
42
Quick Sort Example 𝑝𝑖𝑣𝑜𝑡 , 𝑥=7
𝑖 +1 𝑗
𝐴 6 5 0 2 4 7 8 9
𝑝 𝑟
43
Quick Sort Example
+1
6 5 0 2 4 7 8 9
≤ ¿
44
Quick Sort Example
+1
6 5 0 2 4 7 8 9
≤ ¿
45
Quick Sort Example
+1
6 5 0 2 4 7 8 9
≤ ¿
46
Quick Sort Example
+1
6 5 0 2 4 7 8 9
≤ ¿
47
Quick Sort Space Analysis
Best Case Space Complexity
𝑛
𝑛 𝑛
2 2
𝑛 𝑛 𝑛 𝑛
4 4 4 4
.
.
.
48
Quick Sort Space Analysis
Best Case Space Complexity Worst Case Space Complexity
𝑛 𝑛
𝑛
2
𝑛
2 𝑛 −1 0
𝑛
4
𝑛
4
𝑛
4
𝑛
4 𝑛 −2 0
.
.
.
.
.
.
49
Quick Sort Running Time Analysis
𝑛
𝑇 (𝑛)
𝑛 𝑛
2 2
𝑂(𝑛) 𝑛 𝑛 𝑛 𝑛
𝑛 4 4 4 4
𝑇( )
2
𝑛
𝑇( )
2
50
Quick Sort Running Time Analysis
𝑂(𝑛) 𝑛 −2 0
𝑇 (𝑛 −1) .
𝑇 (0) .
.
……
51
Quick Sort Average Time Analysis
Assuming that keys are random, uniformly distributed.
• The average case running time occurs when pivot can take any
position at each step
• The size of two sub-arrays will vary at each step
• Say the Partition procedure always splits the array into some
constant ratio b-to-a, e.g. 9-to-1
52
Picking the Pivot
• How would you pick one?
• Pivot
• = Median of A[left], A[right], and A[center]
• = median of 6, 8, and 0 55
• = A[left] = 6
Quick Sort: Not Stable Example
𝐴 4 2 1 4 3
𝑝 𝑟
56
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 4 2 1 4 3
𝑝 𝑟
57
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 4 2 1 4 3
𝑝 𝑟
58
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 4 2 1 4 3
𝑝 𝑟
59
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 4 2 1 4 3
𝑝 𝑟
60
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 4 2 1 4 3
𝑝 𝑟
61
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 4 1 4 3
𝑝 𝑟
62
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 4 1 4 3
𝑝 𝑟
63
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 4 1 4 3
𝑝 𝑟
64
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 4 1 4 3
𝑝 𝑟
65
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 1 4 4 3
𝑝 𝑟
66
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 1 4 4 3
𝑝 𝑟
67
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 1 4 4 3
𝑝 𝑟
68
Quick Sort: Not Stable Example
𝑖 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 1 4 4 3
𝑝 𝑟
69
Quick Sort: Not Stable Example
𝑖 +1 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 1 4 4 3
𝑝 𝑟
70
Quick Sort: Not Stable Example
𝑖 +1 𝑗 𝑝𝑖𝑣𝑜𝑡 , 𝑥=3
𝐴 2 1 3 4 4
𝑝 𝑟
71
Quick Sort: Not Stable Example
+1
𝐴 2 1 3 4 4
𝑝 𝑟
𝐴 1 2 3 4 4
𝑝 𝑟
72
Quick Sort: Not Stable Example
+1
𝐴 2 1 3 4 4
𝑝 𝑟
𝐴 1 2 3 4 4 Not Stable
𝑝 𝑟
73
Quick Sort: Final Comments
• What happens when the array contains many duplicate
elements?
• Not stable