



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth analysis of the quicksort algorithm, focusing on the average case scenario. It covers the worst, best, and average cases of the algorithm, and uses intuition and mathematical calculations to understand the behavior of quicksort. The document also includes a recurrence relation that is solved using the substitution method to determine the average running time of quicksort.
Typology: Slides
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















Review: Analyzing Quicksort
Partition is always unbalanced
Partition is balanced
The latter, by far, except...
Yes: Already-sorted input
Review: Analyzing Quicksort
T(n) = 2T(n/2) + Θ(n)
T(n) = Θ(n lg n)
Review: Analyzing Quicksort
(Average Case)
Randomly distributed among the recursion tree
Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1)
What happens if we bad-split root node, then good-split the resulting size (n-1) node?
Review: Analyzing Quicksort
(Average Case)
Analyzing Quicksort: Average Case
All inputs distinct (no repeats)
Slightly different partition() procedure
partition around a random element, which is not included in subarrays
all splits (0:n-1, 1:n-2, 2:n-3, … , n-1:0) equally likely
−
=
−
=
1
0
1
0
n
k
n
k
T k n n
T k T n k n n
T n
Write it on the board
Analyzing Quicksort: Average Case
Guess the answer
Assume that the inductive hypothesis holds
Substitute it in for some value < n
Prove that it follows for n
Analyzing Quicksort: Average Case
Guess the answer
T(n) = O(n lg n)
Assume that the inductive hypothesis holds
Substitute it in for some value < n
Prove that it follows for n
Analyzing Quicksort: Average Case
Guess the answer
T(n) = O(n lg n)
Assume that the inductive hypothesis holds
What’s the inductive hypothesis?
Substitute it in for some value < n
Prove that it follows for n
Analyzing Quicksort: Average Case
Guess the answer
T( n ) = O( n lg n )
Assume that the inductive hypothesis holds
T( n ) ≤ an lg n + b for some constants a and b
Substitute it in for some value < n
What value?
Prove that it follows for n
Analyzing Quicksort: Average Case
Guess the answer
T( n ) = O( n lg n )
Assume that the inductive hypothesis holds
T( n ) ≤ an lg n + b for some constants a and b
Substitute it in for some value < n
The value k in the recurrence
Prove that it follows for n
Note: leaving the same recurrence as the book
What are we doing here?
−
=
−
=
−
=
−
=
−
=
1
1
1
1
1
1
1
0
1
0
lg
lg
lg
lg
n
k
n
k
n
k
n
k
n
k
ak k b n n
n n
b ak k b n
b ak k b n n
ak k b n n
T k n n
T n The recurrence to be solved
What are we doing here?
What are we doing here?
Plug in inductive hypothesis
Expand out the k=0 case
2b/n is just a constant, so fold it into Θ (n)
What are we doing here?
What are we doing here?
Evaluate the summation: b+b+…+b = b (n-1)
The recurrence to be solved
Since n-1<n, 2b(n-1)/n < 2b
n
a
n n n
b k k n
a
b n n
ak k n
ak k b n n
T n
n
k
n
k
n
k
n
k
n
k
−
=
−
=
−
=
−
=
−
=
lg 2
lg
lg
lg
1
1
1
1
1
1
1
1
1
1
Distribute the summation What are we doing here?
This summation gets its own set of slides later