Created
November 6, 2022 16:38
-
-
Save danyashorokh/68775860dd156c371b41cae3f06e86f8 to your computer and use it in GitHub Desktop.
[Python] 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 binary_search_iterative(array, element): | |
mid = 0 | |
start = 0 | |
end = len(array) | |
step = 0 | |
while (start <= end): | |
# print('Subarray in step {}: {}'.format(step, str(array[start:end+1]))) | |
step = step + 1 | |
mid = (start + end) // 2 | |
if element == array[mid]: | |
return mid | |
if element < array[mid]: | |
end = mid - 1 | |
else: | |
start = mid + 1 | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment