Medians and Order Statistics

14
Medians and Order Statistics Teacher: Nguyen Van Tuyen Student: Nguyen Phuong Hoa

Transcript of Medians and Order Statistics

Page 1: Medians and Order Statistics

Medians and Order Statistics

Teacher: Nguyen Van TuyenStudent: Nguyen Phuong Hoa

Page 2: Medians and Order Statistics

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

Page 3: Medians and Order Statistics

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

Page 4: Medians and Order Statistics

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

Page 5: Medians and Order Statistics

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?

Page 6: Medians and Order Statistics

Minimum and maximum

Show that the second smallest of n-elements can be found with n+-2

comparisons in the worst case???

Page 7: Medians and Order Statistics

Selection Problem

Can sort first – (n lg n), but can do better – (n).

Page 8: Medians and Order Statistics

p rq

k i ?? k

Page 9: Medians and Order Statistics

Selection in expected linear time

Worst-case: O(n^2)

Best-case: O(n)

Average case: O(n)

Page 10: Medians and Order Statistics

Selection in worst-case linear time

Page 11: Medians and Order Statistics

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.

Page 12: Medians and Order Statistics

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

Page 13: Medians and Order Statistics

Key points

i-th order statistic

Median

Minimum and maximum

Selection Problem

Page 14: Medians and Order Statistics

Q&A