Median, order statistics
description
Transcript of 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.
12
n
12
n
Randomized algorithm: (9.2) Divide & Conquer
≤A[q] ≥A[q]
k
p q r
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).
910n
109
log 10 1.n n
Analysis:
11
TMergesort(n) = 2 • TMergesort(n/2) + cte • n + cte
Forma Fechada de Recorrência
TMS (n) = 2 • TMS(n/2) + c • n + c= 2 •[ 2 • TMS(n/4) + c • n/2 + c ] + c • n + c = 4 • TMS(n/4) + 2c • n + 3c= 4 •[ 2 • TMS(n/8) + c • n/4 + c ] + 2c • n + 3c= 8 • TMS(n/8) + 3c • n + 7c= ...
TMS (n) = 2i • TMS(n/2i) + i • c • n + (2i-1) • c
Average case:
For upper bound, assume the i-th element always falls in larger side of partition.
1
1
1 1
2 2
1max 1, 1 max ,
1 2 1 2
n
k
n n
n nk k
T n T n T k n k nn
T n T k n T k nn n
if 2
max , if 2
1 2 12 2
1 2 12 2
nk kk n k
nn k k
n n n
n nn n n n
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.
1 1
2 2
n 11 2
1 1
2 2
2
12 1 n n 12 22 2
1 12 2
3 1 4 2
n n
n nk k
n
k k
cT n ck n k n
n n
ck k n
n
n ncn
n
c n nc n nn
nc n cn
1
2
2 n
nk
T n T k nn
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.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.
}
5n
5n
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.
25 10n n
3 10n
3 .10 4n n
T(n) ≤ T(n/5) + T(3n/4) + Θ(n). By substitution: T(n) ≤ cn
3
5 419
20
20
,
cn cnT n n
cnn
cncn n
cn
if c is big enough!
Application of linear-time median algorithm: i-th order statistic:
{Find median xPartition 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).
12
n
12
n
Worst-case Θ(nlgn) quicksort:
{ Find median x and partition;
Recursively sort 2 halves;
}
T(n) = 2T(n/2) + Θ(n)
= Θ(nlgn).