Created
February 26, 2013 22:32
-
-
Save rafalio/5042951 to your computer and use it in GitHub Desktop.
MinMax 3n/2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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