For one element, return it as both extrema without a comparison. Otherwise compare consecutive pairs once, put each smaller element in a candidate array and each larger element in a candidate array . If one element is unpaired, put it in both candidate arrays. This simultaneous minimum and maximum construction computes the minimum of and maximum of by balanced divide-and-conquer algorithms: split the candidate array, recursively find the desired extremum in each half, and compare the two returned values. These extremum routines use one comparison per merge and therefore comparisons on candidates.
Every element excluded from is at least its paired element, so a global minimum remains in ; likewise a global maximum remains in . The recursive extremum routines are correct by structural induction. For even , there are pair comparisons and two searches on candidates, givingFor odd , there are pair comparisons and two searches on candidates, givingTogether with , both cases giveFor powers of two, the equivalent direct divide-and-conquer algorithm returns the ordered pair of extrema for each half, then combines the two minima with one comparison and the two maxima with one comparison. Its base cases must include , rather than performing two unnecessary comparisons on a pair. The recurrence relation is for , whence . Arbitrary near-equal splitting without the initial pairing can exceed the desired comparison bound for other values of ; the pairing construction above handles every positive size.
Articles by others on the same topic
There are currently no matching articles.