-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.py
48 lines (34 loc) · 1.12 KB
/
binary_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from typing import List
def iterativeBinarySearch(sorted_array: List, target) -> int:
low = 0
high = len(sorted_array) - 1
mid = 0
while low <= high:
mid = (low + high) // 2
if sorted_array[mid] > target:
high = mid - 1
elif sorted_array[mid] < target:
low = mid + 1
else:
return mid
return -1
def resursiveBinarySearch(sorted_array: List, target, start_index=0) -> int:
low = 0
high = len(sorted_array) - 1
mid = (low + high) // 2
if len(sorted_array) == 0:
return -1
# base_case:
elif sorted_array[mid] == target:
return mid + start_index
elif sorted_array[mid] > target:
return resursiveBinarySearch(
sorted_array[:mid], target, start_index=start_index
)
elif sorted_array[mid] < target:
return resursiveBinarySearch(
sorted_array[mid + 1 :], target, start_index=start_index + mid + 1
)
if __name__ == "__main__":
print(iterativeBinarySearch([2, 4, 6, 8, 10, 145], 10))
print(resursiveBinarySearch([2, 4, 6, 8, 10, 145], -12))