Created
February 13, 2017 01:33
-
-
Save nickwhite917/31acafb596fd7fb168fa4a6714ced425 to your computer and use it in GitHub Desktop.
Binary Search in Python
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(ele, arr, min = 0, max = None): | |
| if max is None: | |
| max = len(arr) - 1 | |
| half = min + (max - min) / 2 | |
| value = arr[half] | |
| if value == ele: | |
| return half | |
| if ele < value: | |
| max = half | |
| return binary_search(ele, arr, min, max) | |
| if ele > value: | |
| min = half | |
| return binary_search(ele, arr, min, max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment