added binary search

pull/4/head
Gborgman05 2020-12-24 00:32:41 -08:00
parent 14930558d6
commit e014313020
1 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,18 @@
def binary_search(array: list, target: int) -> bool:
""" searches through a sorted list to find a target integer """
mid = len(array) // 2
if len(array) < 1:
return False
if len(array) == 1:
return array[0] == target
if array[mid] < target:
return binary_search(array[mid:], target)
elif array[mid] > target:
return binary_search(array[:mid], target)
else:
return True
if __name__ == "__main__":
print(binary_search([1, 2, 3, 4, 5], 1))
print(binary_search([0, 4, 10, 1000], 10))
print(binary_search([], -2))