Randomized Algorithms: Quick Sort
Randomized Algorithms: Quick Sort
Quick Sort
Lecturer
Department of Computer Science and Engineering
Islamic University of Technology
Motivation
Divide
Divide
• Pick a pivot element x in A
Divide
• Pick a pivot element x in A
• Partition the array into subarrays
Divide
• Pick a pivot element x in A
• Partition the array into subarrays
Divide
• Pick a pivot element x in A
• Partition the array into subarrays
• Recurse on L and G
Divide
• Pick a pivot element x in A
• Partition the array into subarrays
• Recurse on L and G
Combine: Trivial
1
Probability of picking a good pivot: 2
1
Probability of picking a good pivot: 2
1
Expected repeats: p =2
MBH (CSE, IUT) Randomized Algorithms 6/8
Paranoid Quick Sort
Runtime Analysis
Runtime Analysis
n 3n
T (n) = T 4 +T 4 + Expected repeats × cn
Runtime Analysis
n 3n
T (n) = T 4 +T 4 + Expected repeats × cn
n 3n
T (n) = T 4 +T 4 + 2cn
Runtime Analysis
n 3n
T (n) = T 4 +T 4 + Expected repeats × cn
n 3n
T (n) = T 4 +T 4 + 2cn
2cn
2cn 6cn
4 4
Runtime Analysis
n 3n
T (n) = T 4 +T 4 + Expected repeats × cn
n 3n
T (n) = T 4 +T 4 + 2cn
2cn
2cn 6cn
4 4
< 2cn
Runtime Analysis
n 3n
T (n) = T 4 +T 4 + Expected repeats × cn
n 3n
T (n) = T 4 +T 4 + 2cn
2cn
2cn 6cn
4 4
O(log4 (2cn))
2cn 6cn 6cn 18cn
16 16 16 16
.. .. .. .. .. .. ..
. . . . . . .
< 2cn
Runtime Analysis
n 3n
T (n) = T 4 +T 4 + Expected repeats × cn
n 3n
T (n) = T 4 +T 4 + 2cn
2cn
2cn 6cn
4 4
O(log4 (2cn)) O(log 4 (2cn))
3
2cn 6cn 6cn 18cn
16 16 16 16
.. .. .. .. .. .. ..
. . . . . . .
< 2cn
Runtime Analysis
n 3n
T (n) = T 4 +T 4 + Expected repeats × cn
n 3n
T (n) = T 4 +T 4 + 2cn
2cn 2cn
2cn 6cn
4 4 2cn
O(log4 (2cn)) O(log 4 (2cn))
3
2cn 6cn 6cn 18cn
16 16 16 16 2cn
.. .. .. .. .. .. ..
. . . . . . .
< 2cn
Runtime Analysis
n 3n
T (n) = T 4 +T 4 + Expected repeats × cn
n 3n
T (n) = T 4 +T 4 + 2cn
2cn 2cn
2cn 6cn
4 4 2cn
O(log4 (2cn)) O(log 4 (2cn))
3
2cn 6cn 6cn 18cn
16 16 16 16 2cn
.. .. .. .. .. .. ..
. . . . . . .
< 2cn
CLRS 7.0-7.4
Dasgupta 2.4
Erickson 1.5, 1.8