Last active
January 21, 2020 09:12
-
-
Save Foxonn/8283f0fa2474d5f107a738d58906f387 to your computer and use it in GitHub Desktop.
Python | My variation binary search
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 my_binary_search(_array, item): | |
n = 0 # iteration | |
i = 0 # finding index | |
array = _array[:] | |
if item > _array[-1] or item < _array[0]: | |
return None | |
while len(array) != 1: | |
n += 1 | |
mid = int(len(array) / 2) | |
i += mid | |
if item >= array[mid]: | |
array = array[mid:] | |
else: | |
array = array[:mid] | |
i -= mid | |
return "iteration: {} position: {} value: {} target: {}".format(*[n, i, _array[i], item]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment