Created
July 23, 2020 20:32
-
-
Save shakeelmajeed-work/2247d5e857bb7f4e98329e70b643df27 to your computer and use it in GitHub Desktop.
Binary Search in Python 3!
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 dosearch(array, target): | |
min = 0 | |
max = len(array) - 1 | |
array = sorted(array) | |
while min<=max: | |
guess = (min+max) // 2 | |
if array[guess] == target: | |
return guess | |
elif array[guess] < target: | |
min = guess + 1 | |
else: | |
max = guess - 1 | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment