-
-
Save monkey-codes/d43816c846785bb691f9062f963037fd to your computer and use it in GitHub Desktop.
Python simple binary search
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 binary_search(input_array, value): | |
| low = 0 | |
| high = len(input_array) -1 | |
| while low <= high: | |
| middle_idx = (low + high)/2 | |
| middle = input_array[middle_idx] | |
| if value < middle: | |
| high = middle_idx - 1 | |
| elif value > middle: | |
| low = middle_idx + 1 | |
| else: | |
| return middle_idx | |
| return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment