Created
October 24, 2018 20:46
-
-
Save arzon94/b56b2d50f4c37189c75e5576ed5bb631 to your computer and use it in GitHub Desktop.
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(arr): | |
| low, high, mid = 0, len(arr) - 1, 0 | |
| best = low | |
| while low <= high: | |
| mid = low + (high - low) / 2 | |
| if arr[mid] < val: | |
| low = mid + 1 | |
| elif arr[mid] > val: | |
| high = mid - 1 | |
| else: | |
| best = mid | |
| break | |
| if abs(arr[mid] - val) < abs(arr[best] - val): | |
| best = mid | |
| return best |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment