Given an unsorted array, extract the max and min value using the least number of comparison.
Sigiloso
Minimum number of comparisons is 3n/2. The strategy is to go through the elements in pairs, and compare the smaller one to the minimum, and the bigger one to the maximum. This is 3 comparisons, done n/2 times in total, for 3n/2 running time. Working code in python: import random import sys r = [random.randint(1,100) for i in range(31)] print r mini = r[0] maxi = r[0] start = 0 if len(r) % 2 != 0: start = 1 for i in range(start,len(r)-1,2): n1 = r[i] n2 = r[i+1] # Exactly 3 comparisons each time if(n1 < n2): mini = min(n1,mini) maxi = max(n2,maxi) else: mini = min(n2,mini) maxi = max(n1,maxi) print mini print maxi