-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.py
41 lines (39 loc) · 1012 Bytes
/
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
# Binary search
def binary_search(arr, e):
# Note that binary search are only for sorted arrays
# ub = upper bound
# lb = lower bound
ub = len(arr)
lb = 0
found = False
while not found:
mid = round((ub + lb) / 2)
if lb > len(arr):
print("Not found")
break
if ub < 0:
print("Not found")
break
if e == arr[mid]:
print(f"Found at possition {mid + 1}")
found = True
elif e > arr[mid]:
lb += 1
else:
ub -= 1
# Below code is just an illustration for the code runner to see how the function works.
a_list = [2, 4, 8, 16, 32, 64, 128]
try:
while True:
opt = int(input("""
Choose an option:
1. Search for an element
2. exit
> """))
if opt == 1:
element = int(input("Enter the element you looking for: "))
binary_search(a_list, element)
elif opt == 2:
break
except:
print("Error")