Download presentation

Presentation is loading. Please wait.

1
Median, order statistics

2
Problem Find the i-th smallest of n elements. i=1: minimum i=n: maximum i= or i= : median Sol: sort and index the i-th element. Ο(nlgn): worst case.

3
Randomized algorithm: (9.2) Divide & Conquer ≤A[q]≥A[q] k pq r

4
Analysis: Lucky case: T(n) ≤ T( ) + Θ(n) = Θ(n). By case 3 of master method (chap 4) Unlucky case: T(n) = T(n-1) + Θ(n) = Θ(n 2 ).

5
Analysis:

6
Average case: For upper bound, assume the i-th element always falls in larger side of partition.

7
Solve By substitution method: assume T(n) ≤ cn. We can pick c large enough so that c(n/4+1/2) dominates the Ο(n) term.

8
Worst case linear-time order statistics (9.3) Theoretical interest Idea: generate a good partitioning element x.

9
Select (i) { Divide n elements into groups of 5 elements. Find the median of each group of 5. Use Select recursively to find the median x of the medians. Partition the n elements around x Let k = rank of x. 5. If i=k then return x. If i<k then use Select recursively to find the i-th smallest in the 1st part else use Select recursively to find (i-k)th smallest in the last part. }

11
Analysis: At least ½ of 5-element medians ≤ x, which is at least medians. At least elements ≤ x. For n ≥ 50, Similarly, at least n/4 elements ≥ x. Thus after partitioning around x, step 5 is called on ≤ 3n/4 elements.

12
T(n) ≤ T(n/5) + T(3n/4) + Θ(n). By substitution: T(n) ≤ cn

13
Application of linear-time median algorithm: i-th order statistic: { Find median x Partition input around x If i≤ then recursively find the i-th element in the first half. else recursively find (i- )th element in the 2nd half. } T(n) = T(n/2) + Θ(n) = Θ(n).

14
Worst-case Θ(nlgn) quicksort: { Find median x and partition; Recursively sort 2 halves; } T(n) = 2T(n/2) + Θ(n) = Θ(nlgn).

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google