Last active
September 27, 2015 10:58
-
-
Save martinusso/1259292 to your computer and use it in GitHub Desktop.
Recursive binary search in Python
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 get_index_recursive(vector, number, index = -1, prior_index = 0): | |
index_helper = index if index > -1 else int(len(vector)/2) | |
if vector[index_helper] < number: | |
prior_index_helper = prior_index if prior_index > 0 else len(vector) | |
index_helper = int((prior_index_helper - index_helper)/2) + index_helper | |
elif vector[index_helper] > number: | |
prior_index_helper = index_helper +1 | |
index_helper = int(index_helper/2) | |
else: | |
return index_helper | |
return get_index_recursive(vector, number, index_helper, prior_index_helper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment