Medians and Order Statistics
-
Upload
hoa-nguyen -
Category
Education
-
view
182 -
download
0
Transcript of Medians and Order Statistics
Medians and Order Statistics
Teacher: Nguyen Van TuyenStudent: Nguyen Phuong Hoa
Outline:
1. i-th order statistic
2. Minimum and maximum
3. Selection Problema. Selection in expected linear timeb. Selection in worst-case linear time
4.Q&A
i-th order statistic
The i-th order statistic is the i-th smallest element of a sorted array.
8th order statistic
3 4 13 14 21 27 41 54 65 75
Median
Median is a halfway point of the set.
N is odd, median is (n+1)/2-th order statistic
N is even,
upper median
3 4 13 14 23 27 41
lower median
54 65 75
The lower median is the -th order statisticThe upper median is the -th order statistic
Minimum and maximum
Can do with 2n-2 comparisons.
Can do better 3 Form pairs of elements Compare elements in each pair Pair (ai, ai+1), assume ai < ai+1, then
Compare (min,ai), (ai+1,max) 3 comparisions for each pair.
How many comparisons are necessary to determine the minimum and
maximum of a set of n-elements?
Minimum and maximum
Show that the second smallest of n-elements can be found with n+-2
comparisons in the worst case???
Selection Problem
Can sort first – (n lg n), but can do better – (n).
p rq
k i ?? k
Selection in expected linear time
Worst-case: O(n^2)
Best-case: O(n)
Average case: O(n)
Selection in worst-case linear time
Selection in worst-case linear time
1. Group the given number in subsets of 5 in O(n) time
2. Find the median of each of the n=5 groups and then picking the median from the sorted list of group elements.
3. Use SELECT recursively to find the median x of the n=5 medians found in step 2
4. Partition the input array around the median-of-median x using the modified version of PARTITION. Let k be one more than the number of elements on the low side of the partition, so that x is the kth smallest element and there are n-k elements on the high side of the partition.
5. If i = k, then return x. Otherwise, use SELECT recursively to find the ith smallest element on the low side if i<k,or the (i-k)th smallest element on the high side if i>k.
If n is small, (n<6) just sort and return the k-th smallest number in constant time O(1) time.
2 5 64 24 44
1 4 6 9 20
21 95 36 8 7
4 24 3 56 8
12 13 17 18 89
1 4 3 8 7
2 5 6 9 8
4 13 17 18 20
12 24 36 24 44
21 95 64 56 89
Key points
i-th order statistic
Median
Minimum and maximum
Selection Problem
Q&A