Created
September 3, 2021 03:20
-
-
Save harendra21/45b59f286d324abbb6d89e17b06078e8 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
def binary_search(item_list,item): | |
first = 0 | |
last = len(item_list)-1 | |
found = False | |
while( first<=last and not found): | |
mid = (first + last)/2 | |
if item_list[mid] == item : | |
found = True | |
else: | |
if item < item_list[mid]: | |
last = mid - 1 | |
else: | |
first = mid + 1 | |
return found | |
print binary_search([1,2,3,5,8],6) | |
print binary_search([1,2,3,5,8],5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment