Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Binary Search - Python/Binary Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def binary_search(values, i, j, key):
if not i < j:
return -1
mid = (i + j) // 2
if values[mid] < key:
return binary_search(values, mid + 1, j, key)
elif values[mid] > key:
return binary_search(values, i, mid, key)
else:
return mid


values = input('Enter the sorted numbers: \n')
values = values.split()
values = [int(x) for x in values]
key = int(input('The number to search for: '))

index = binary_search(values, 0, len(values), key)
if index < 0:
print('{} was not found.'.format(key))
else:
print('{} was found at index {}.'.format(key, index))