Medians and Order Statistics
If A = <a1, a2, a3,
...,an>
length [A] = n
Sorted <a1¢, a2¢,
a3¢, ...,an¢>
where a1¢ £ a2¢
£ a3 ¢£ ...£
an¢
ai¢ = ith
order statistic
a1¢ = first order
statistic (minimum)
an¢ = nth
order statistic (maximum)
"middle guy" « median
( n + 1 ) / 2 = i n odd
n/2 and n/2 + 1 = i n even
i = ë ( n + 1 ) / 2 û
and é ( n + 1 ) / 2 ù
Assume distinct values for the ai¢
: a1¢ £ a2¢
£ a3 ¢£ ...£
an
Selection Problem
Input : A (n distinct), 1 £ i £
n
Output : ai¢ (element
of A larger than exactly i - 1 other elements of A).
Best Algorithms we know:
User heap sort or merge sort to sort in O ( (n lg n
)) time and index ai¢ . We can do
better than O (( n) ), we'll see how now.
Minimum and Maximum
Using comparisons can find the minimum in at most n - 1
comparisons:
Minimum (A)
-
min ¬ A[1]
-
for i ¬ 2 to length[A] =
n ( for loop executed n- 1 times )
-
do if min > A[i]
-
then min ¬
A[i] ( Q ( lg n
) )times
-
return min
If we desire both maximum and minimum we can add max A ¬
[1] and comparisons to 2 n - 2 comparisons for both. We can get
away with 3 é n / 2 ù
comparisons by ( A[i], A[ i+1] ) taking pairs and comparing
those and minimum/maximum of those are compared with running values. There
are three compares per tow elements of A.
|