2. 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).
3. 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.
4. 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.
5. 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.
6. How many comparisons are necessary and
sufficient for computing both the minimum
and 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…..
7. Simultaneous Minimum and Maximum
Simultaneous 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.
8. 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
9. 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)
10. …
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)
1. x=A[r]
2. i=p-1
3. for j=p to r-1
4. if A[j]≤x
5. i=i+1
6. exchange A[i] with a[j]
7. exchange A[i+1] with A[r]
8. return i+1