median and order statistics

18
Medians and Order Statistics

Upload: shashank-singh

Post on 07-Aug-2015

54 views

Category:

Education


2 download

TRANSCRIPT

Medians and Order Statistics

Order statistics

ith order statistic is the ith smallest element of a set of n elements.

We will consider some special cases of the order statistics :

The minimum, i.e. the first(i=1), the maximum, i.e. the last(i=n), and the median, i.e. the “halfway point."

If n is odd, the median is unique, and if n is even, there are two medians(lower median and upper median).

Selection problem

It is a problem of computing where

Input: A set A of n distinct numbers and a number i, with 1 i n.

Output: the element x A that is larger than exactly i – 1 other elements of A.

Naïve Solution

We can simply sort the given numbers using heap or merge sort and then simply giving the output as ith index of the array.

If we use a sorting algorithm then the selection problem can be solved in in O(n lg n) time.

But using a sorting is more like using a cannon to shoot a fly since only one number needs to computed.

So we will just find out that number. Before that let us solve the problem of selecting the minimum and maximum of a given set of number.

Minimum and Maximum

Minimum (A)

1. min = A[1]

2. for i = 2 to A.length

3. if min > A[i]

4. Min = A[i]

5. return min

Maximum can be determined similarly.

How many comparisons are necessary and sufficient for computing both the minimumand the maximum?

Well, to compute the minimum n-1 comparisons are necessary and sufficient. The same is true for the maximum. So, the number should be 2n-2 for computing both.

Is it possible to find out the minimum and maximum in less than 2n-2 comparisons?

YES , lets see how…..

Simultaneous Minimum and MaximumSimultaneous computation of maximum and minimum can be done in at most 3n/2 steps.

Idea: Maintain the variables min and max. Process the n numbers in pairs. For the first pair, set min to the smaller and max to the other. After that, for each new pair, compare the smaller with min and the larger with max.

Lets see the algorithm.

MAX-AND-MIN(A , n)

1. max = A[n], min = A[n]

2. for i = 1 to n/2

3. if A[2i - 1] ≥ A[2i]

4. { if A[2i - 1] > max

5. max = A[2i - 1]

6. if A[2i] < min

7. min = A[2i] }

8. else { if A[2i] > max

9. max = A[2i]

10. if A[2i - 1] < min

11. min A[2i - 1] }

12. return max and min

Randomized-Select

Randomized-Select (A, p, r, i)

1. if p = r

2. return A[p]

3. q = Randomized-Partition(A, p, r)

4. k =q – p + 1

5. if i = k

6. return A[q]

7. else if i < k

8. return Randomized-Select(A, p, q – 1, i)

9. else return Randomized-Select(A, q+1, r, i – k)

…Randomized-Partition(A, p, r)

1. i = random(p,r)

2. exchange A[r] with A[i]

3. return Partition(A, p, r)

Partition(A, p, r)

4. x=A[r]

5. i=p-1

6. for j=p to r-1

7. if A[j]≤x

8. i=i+1

9. exchange A[i] with a[j]

10.exchange A[i+1] with A[r]

11.return i+1

Example

6 10 13 5 8 3 2 11

i=7

Analysis

Thank You !