Lecture 9
Lecture 9
First approach
First approach
Sort the X and return the i th element.
First approach
Sort the X and return the i th element.
Time Complexity = Θ(n log n)
Second Approach
First approach
Sort the X and return the i th element.
Time Complexity = Θ(n log n)
Second Approach
Divide and conquer approach.
First approach
Sort the X and return the i th element.
Time Complexity = Θ(n log n)
Second Approach
Divide and conquer approach.
Apply the RANDOMISED PARTITION from quick sort on the
array by selecting a pivot.
First approach
Sort the X and return the i th element.
Time Complexity = Θ(n log n)
Second Approach
Divide and conquer approach.
Apply the RANDOMISED PARTITION from quick sort on the
array by selecting a pivot.
Once the partition ends the array is divided into two parts
with (k − 1) elements on left array and (n − k) on right array.
First approach
Sort the X and return the i th element.
Time Complexity = Θ(n log n)
Second Approach
Divide and conquer approach.
Apply the RANDOMISED PARTITION from quick sort on the
array by selecting a pivot.
Once the partition ends the array is divided into two parts
with (k − 1) elements on left array and (n − k) on right array.
If k= i, then RETURN.
First approach
Sort the X and return the i th element.
Time Complexity = Θ(n log n)
Second Approach
Divide and conquer approach.
Apply the RANDOMISED PARTITION from quick sort on the
array by selecting a pivot.
Once the partition ends the array is divided into two parts
with (k − 1) elements on left array and (n − k) on right array.
If k= i, then RETURN.
If k < i, recursively call the RANDOMISED PARTITION on
right subarray.
First approach
Sort the X and return the i th element.
Time Complexity = Θ(n log n)
Second Approach
Divide and conquer approach.
Apply the RANDOMISED PARTITION from quick sort on the
array by selecting a pivot.
Once the partition ends the array is divided into two parts
with (k − 1) elements on left array and (n − k) on right array.
If k= i, then RETURN.
If k < i, recursively call the RANDOMISED PARTITION on
right subarray.
otherwise call on left subarray.
Prof. Prateek Vishnoi Medians and Order Statistics
Complexity Analysis
Best Case
Best Case
Best Case occurs when the pivot selected is the i th order
statistic.
Best Case
Best Case occurs when the pivot selected is the i th order
statistic.
Recurrence Relation
Best Case
Best Case occurs when the pivot selected is the i th order
statistic.
Recurrence Relation
Best Case
Best Case occurs when the pivot selected is the i th order
statistic.
Recurrence Relation
Best Case
Best Case occurs when the pivot selected is the i th order
statistic.
Recurrence Relation
Worst Case
Worst Case
Worst Case occurs when the pivot selected divides array into
two parts of size (n − 1) and 0 and pivot is not the i th order
statistic.
Worst Case
Worst Case occurs when the pivot selected divides array into
two parts of size (n − 1) and 0 and pivot is not the i th order
statistic.
Recurrence Relation
T (n) = T (n − 1) + Θ(n)
Worst Case
Worst Case occurs when the pivot selected divides array into
two parts of size (n − 1) and 0 and pivot is not the i th order
statistic.
Recurrence Relation
T (n) = T (n − 1) + Θ(n)
Worst Case
Worst Case occurs when the pivot selected divides array into
two parts of size (n − 1) and 0 and pivot is not the i th order
statistic.
Recurrence Relation
T (n) = T (n − 1) + Θ(n)
Worst Case
Worst Case occurs when the pivot selected divides array into
two parts of size (n − 1) and 0 and pivot is not the i th order
statistic.
Recurrence Relation
T (n) = T (n − 1) + Θ(n)
T (n) = (n − 1) + T (X )
T (n) = (n − 1) + T (X )
where X is a random variable s.t, 0 ≤ X ≤ (n − 1)
T (n) = (n − 1) + T (X )
where X is a random variable s.t, 0 ≤ X ≤ (n − 1)
T (n) = (n − 1) + E [T (X )]
T (n) = (n − 1) + T (X )
where X is a random variable s.t, 0 ≤ X ≤ (n − 1)
T (n) = (n − 1) + E [T (X )]
Possible splits of array :
(0, n−1), (1, n−2), (2, n−3) . . . (n/2−2, n/2+1), (n/2−1, n/2)
T (n) = (n − 1) + T (X )
where X is a random variable s.t, 0 ≤ X ≤ (n − 1)
T (n) = (n − 1) + E [T (X )]
Possible splits of array :
(0, n−1), (1, n−2), (2, n−3) . . . (n/2−2, n/2+1), (n/2−1, n/2)
T (n) = (n − 1) + T (X )
where X is a random variable s.t, 0 ≤ X ≤ (n − 1)
T (n) = (n − 1) + E [T (X )]
Possible splits of array :
(0, n−1), (1, n−2), (2, n−3) . . . (n/2−2, n/2+1), (n/2−1, n/2)
T (n) = (n − 1) + T (X )
where X is a random variable s.t, 0 ≤ X ≤ (n − 1)
T (n) = (n − 1) + E [T (X )]
Possible splits of array :
(0, n−1), (1, n−2), (2, n−3) . . . (n/2−2, n/2+1), (n/2−1, n/2)
T (n) = (n − 1) + T (X )
where X is a random variable s.t, 0 ≤ X ≤ (n − 1)
T (n) = (n − 1) + E [T (X )]
Possible splits of array :
(0, n−1), (1, n−2), (2, n−3) . . . (n/2−2, n/2+1), (n/2−1, n/2)
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Recursively find the true median of the medians. Call this p.
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Recursively find the true median of the medians. Call this p.
Use p as a pivot to partition the array.
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Recursively find the true median of the medians. Call this p.
Use p as a pivot to partition the array.
Recurse on the appropriate piece in the same way as previous.
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Recursively find the true median of the medians. Call this p.
Use p as a pivot to partition the array.
Recurse on the appropriate piece in the same way as previous.
Example
A = {1,2,3,10,11,4,5,6,12,13,7,8,9,14,15}
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Recursively find the true median of the medians. Call this p.
Use p as a pivot to partition the array.
Recurse on the appropriate piece in the same way as previous.
Example
A = {1,2,3,10,11,4,5,6,12,13,7,8,9,14,15}
{ 1,2,3,10,11} , {4,5,6,12,13}, { 7,8,9,14,15}
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Recursively find the true median of the medians. Call this p.
Use p as a pivot to partition the array.
Recurse on the appropriate piece in the same way as previous.
Example
A = {1,2,3,10,11,4,5,6,12,13,7,8,9,14,15}
{ 1,2,3,10,11} , {4,5,6,12,13}, { 7,8,9,14,15}
{ 3,6,9}
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Recursively find the true median of the medians. Call this p.
Use p as a pivot to partition the array.
Recurse on the appropriate piece in the same way as previous.
Example
A = {1,2,3,10,11,4,5,6,12,13,7,8,9,14,15}
{ 1,2,3,10,11} , {4,5,6,12,13}, { 7,8,9,14,15}
{ 3,6,9}
p ={6}
Algorithm
I/P : Array A of size n and positive integer k ≤ n
Group the array into n/5 groups and find the median of each
group.
Recursively find the true median of the medians. Call this p.
Use p as a pivot to partition the array.
Recurse on the appropriate piece in the same way as previous.
Example
A = {1,2,3,10,11,4,5,6,12,13,7,8,9,14,15}
{ 1,2,3,10,11} , {4,5,6,12,13}, { 7,8,9,14,15}
{ 3,6,9}
p ={6}
5 elements at left and 9 elements at right.
Worst Case
Worst Case
Step 1 takes O(n) time.
Worst Case
Step 1 takes O(n) time.
Step 2 takes atmost T (n/5) time.
Worst Case
Step 1 takes O(n) time.
Step 2 takes atmost T (n/5) time.
Step 3(partitioning) takes O(n) time.
Worst Case
Step 1 takes O(n) time.
Step 2 takes atmost T (n/5) time.
Step 3(partitioning) takes O(n) time.
Assume for now, that pivot divides the array of size atmost
7n/10
T (n) ≤ cn + T (n/5) + T (7n/10)
Worst Case
Step 1 takes O(n) time.
Step 2 takes atmost T (n/5) time.
Step 3(partitioning) takes O(n) time.
Assume for now, that pivot divides the array of size atmost
7n/10
T (n) ≤ cn + T (n/5) + T (7n/10)
T (n) = O(n)