140 likes | 328 Views
Median, order statistics. 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. k. ≤A[q]. ≥A[q]. p. q. r. Randomized algorithm: (9.2). Divide & Conquer. Analysis:.
E N D
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.
k ≤A[q] ≥A[q] p q r Randomized algorithm: (9.2) • Divide & Conquer
Analysis: • Lucky case: T(n) ≤ T( ) + Θ(n) = Θ(n). By case 3 of master method (chap 4) • Unlucky case: T(n) = T(n-1) + Θ(n) = Θ(n2).
Average case: For upper bound, assume the i-th element always falls in larger side of partition.
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.
Worst case linear-time order statistics (9.3) • Theoretical interest • Idea: generate a good partitioning element x.
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. • 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. }
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.
T(n) ≤ T(n/5) + T(3n/4) + Θ(n). • By substitution: T(n) ≤ cn
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).
Worst-case Θ(nlgn) quicksort: { Find median x and partition; Recursively sort 2 halves; } T(n) = 2T(n/2) + Θ(n) = Θ(nlgn).