0% found this document useful (0 votes)
29 views

9.1 Minimum and Maximum: Length (A) N. A) Min A (1) I 2 To Length (A) Min A (I) Min A (I) Min

This document discusses algorithms for finding the minimum, maximum, and other order statistics of a data set. It presents an optimal algorithm to find the minimum of a data set in n-1 comparisons and shows how to find both the minimum and maximum in 2n-1 comparisons. It then describes an algorithm to find both the minimum and maximum using only 3 floor(n/2) comparisons by processing elements in pairs. The document goes on to discuss how to find the kth smallest element in expected linear time using a randomized selection algorithm modeled after quicksort.

Uploaded by

Soe Sandar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

9.1 Minimum and Maximum: Length (A) N. A) Min A (1) I 2 To Length (A) Min A (I) Min A (I) Min

This document discusses algorithms for finding the minimum, maximum, and other order statistics of a data set. It presents an optimal algorithm to find the minimum of a data set in n-1 comparisons and shows how to find both the minimum and maximum in 2n-1 comparisons. It then describes an algorithm to find both the minimum and maximum using only 3 floor(n/2) comparisons by processing elements in pairs. The document goes on to discuss how to find the kth smallest element in expected linear time using a randomized selection algorithm modeled after quicksort.

Uploaded by

Soe Sandar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

184 Chapter 9 Medians and Order Statistics

9.1 Minimum and maximum

How many comparisons are necessary to determine the minimum of a set of n


elements? We can easily obtain an upper bound of n − 1 comparisons: examine
each element of the set in turn and keep track of the smallest element seen so
far. In the following procedure, we assume that the set resides in array A, where
length[A] = n.

M INIMUM ( A)
1 min ← A[1]
2 for i ← 2 to length[A]
3 do if min > A[i]
4 then min ← A[i]
5 return min

Finding the maximum can, of course, be accomplished with n − 1 comparisons as


well.
Is this the best we can do? Yes, since we can obtain a lower bound of n − 1
comparisons for the problem of determining the minimum. Think of any algorithm
that determines the minimum as a tournament among the elements. Each compar-
ison is a match in the tournament in which the smaller of the two elements wins.
The key observation is that every element except the winner must lose at least one
match. Hence, n − 1 comparisons are necessary to determine the minimum, and
the algorithm M INIMUM is optimal with respect to the number of comparisons
performed.

Simultaneous minimum and maximum


In some applications, we must find both the minimum and the maximum of a set
of n elements. For example, a graphics program may need to scale a set of (x, y)
data to fit onto a rectangular display screen or other graphical output device. To do
so, the program must first determine the minimum and maximum of each coordi-
nate.
It is not difficult to devise an algorithm that can find both the minimum and the
maximum of n elements using 2(n) comparisons, which is asymptotically optimal.
Simply find the minimum and maximum independently, using n − 1 comparisons
for each, for a total of 2n − 2 comparisons.
In fact, at most 3 ⌊n/2⌋ comparisons are sufficient to find both the minimum and
the maximum. The strategy is to maintain the minimum and maximum elements
seen thus far. Rather than processing each element of the input by comparing it
against the current minimum and maximum, at a cost of 2 comparisons per element,
9.2 Selection in expected linear time 185

we process elements in pairs. We compare pairs of elements from the input first
with each other, and then we compare the smaller to the current minimum and the
larger to the current maximum, at a cost of 3 comparisons for every 2 elements.
Setting up initial values for the current minimum and maximum depends on
whether n is odd or even. If n is odd, we set both the minimum and maximum
to the value of the first element, and then we process the rest of the elements in
pairs. If n is even, we perform 1 comparison on the first 2 elements to determine
the initial values of the minimum and maximum, and then process the rest of the
elements in pairs as in the case for odd n.
Let us analyze the total number of comparisons. If n is odd, then we perform
3 ⌊n/2⌋ comparisons. If n is even, we perform 1 initial comparison followed by
3(n − 2)/2 comparisons, for a total of 3n/2 − 2. Thus, in either case, the total
number of comparisons is at most 3 ⌊n/2⌋.

Exercises

9.1-1
Show that the second smallest of n elements can be found with n + ⌈lg n⌉ − 2
comparisons in the worst case. (Hint: Also find the smallest element.)

9.1-2 ⋆
Show that ⌈3n/2⌉ − 2 comparisons are necessary in the worst case to find both the
maximum and minimum of n numbers. (Hint: Consider how many numbers are
potentially either the maximum or minimum, and investigate how a comparison
affects these counts.)

9.2 Selection in expected linear time

The general selection problem appears more difficult than the simple problem of
finding a minimum. Yet, surprisingly, the asymptotic running time for both prob-
lems is the same: 2(n). In this section, we present a divide-and-conquer algorithm
for the selection problem. The algorithm R ANDOMIZED -S ELECT is modeled af-
ter the quicksort algorithm of Chapter 7. As in quicksort, the idea is to partition
the input array recursively. But unlike quicksort, which recursively processes both
sides of the partition, R ANDOMIZED -S ELECT only works on one side of the parti-
tion. This difference shows up in the analysis: whereas quicksort has an expected
running time of 2(n lg n), the expected time of R ANDOMIZED -S ELECT is 2(n).
R ANDOMIZED -S ELECT uses the procedure R ANDOMIZED -PARTITION intro-
duced in Section 7.3. Thus, like R ANDOMIZED -Q UICKSORT, it is a randomized al-
gorithm, since its behavior is determined in part by the output of a random-number

You might also like