MAXMIN( S ) Assumes that n = 2m
if |S| = 2 then
let S = {a,b}
return < MAX(a,b), MIN(a,b) > needs only 1 comparison
else
divide S into S1 and S2, each with ½ the elements
< max1, min1 > := MAXMIN( S1 )
< max2, min2 > := MAXMIN( S2 )
return < MAX(max1,max2), MIN(min1,min2) >
Comparisons are made only by the MAX or MIN functions.
Let T(n) = the number of comparisons
needed by MAXMIN when |S| = n.
T(2) = 1, since MIN(a,b) = a+b-MAX(a,b).
For n > 2, T(n) = 2T(n/2) + 2.
Solve recurrence:
We expect that T(n) will be linear in n, so assume that T(n) = an + b.
T(2) = 1 → 2a + b = 1
T(n) = 2T(n/2) + 2 →
an + b = 2(an/2 + b) + 2
This second equation gives: b = 2b + 2,
and therefore b = -2.
Plugging that into the first equation gives: 2a - 2 = 1,
and therefore a = 3/2.
Thus, T(n) = (3/2)n - 2.