Created
July 10, 2017 23:22
-
-
Save sreevidyavutukuru/caed16bd47eb3e35ff2a9e94011f764f to your computer and use it in GitHub Desktop.
This file contains 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
import math | |
def index(list1, n): | |
startIndex = 0 | |
endIndex = len(list1) - 1 | |
while startIndex <= endIndex: | |
mid = startIndex + int(math.floor((endIndex-startIndex)/2)) | |
if list1[mid] == n: | |
print n | |
print mid | |
break | |
elif n > list1[mid]: | |
startIndex = mid + 1 | |
else: | |
endIndex = mid | |
index([1,3,4,5,6,8,9,10,11,12],10) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mid = (endIndex+startIndex)/2