1 / 14

Median, order statistics

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:.

forbes
Download Presentation

Median, order statistics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. k ≤A[q] ≥A[q] p q r Randomized algorithm: (9.2) • Divide & Conquer

  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) = Θ(n2).

  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. • 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. }

  10. 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.

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

  12. 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).

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

More Related