Skip to content

Instantly share code, notes, and snippets.

@rafalio
Created February 26, 2013 22:32
Show Gist options
  • Save rafalio/5042951 to your computer and use it in GitHub Desktop.
Save rafalio/5042951 to your computer and use it in GitHub Desktop.
MinMax 3n/2
def getMinMax(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)
return [mini,maxi]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment