SELECTION AND ADVERSARY ARGUMENTS

Tournaments:

FINDING max AND min.  Max and Min refer to the largest and smallest keys respectively in a set of n keys.

Algorithm to find MAX.

Input:    E, an array of numbers, defined for indices 0, ......, n - 1; n >= 1, hte number of entries.

Outpt:    Returns max, the largest entry in E.

    int    findMax(E, n)
    1.    max = E[0];
    2.    for (index =1; index < n; index ++)
    3.        if (max < E[index])
    4.            max = E[index];
    5.    return max;

By converting > to <, the algorithm can be used to find the smallest value.

Sometimes we need not just the largest but also the second largest.

Algorithm modified to find the largest and the second largeest keys in an arrray E of n keys by sequentially scanning the array and keeping track of the two largest keys seen so far.

if    (E[1] > E[2])
    max = E[1];
    second = E[2];
else
    max = E[2];
    second = E[1];
for (i = 3; i <=n; i++)
    if (E[i] > second)
        if (E[i] > max)
            second = max;
            max = E[i];
        else
           second = E[i];