Skip to content

Instantly share code, notes, and snippets.

@Foxonn
Last active January 21, 2020 09:12
Show Gist options
  • Save Foxonn/8283f0fa2474d5f107a738d58906f387 to your computer and use it in GitHub Desktop.
Save Foxonn/8283f0fa2474d5f107a738d58906f387 to your computer and use it in GitHub Desktop.
Python | My variation binary search
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