-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search.py
More file actions
39 lines (31 loc) · 965 Bytes
/
Copy pathBinary_Search.py
File metadata and controls
39 lines (31 loc) · 965 Bytes
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
def main():
def binarySearch(array, x):
low = 0
high = len(array) - 1
found = False
out = 0
if x < array[low] or x > array[high]:
out = -1
found = True
while not found:
mid = (high + low)//2
if array[mid] == x:
out = mid
found = True
elif array[mid] < x:
low = mid + 1
else:
high = mid - 1
return out
def userInput():
highNum = int(input("high: "))
lowNum = int(input("low: "))
array = []
for count in range(highNum - lowNum + 1):
array.append(lowNum + count)
return array
array = userInput()
print(f"Your array: {array}")
searchFor = int(input("search value: "))
print(binarySearch(array, searchFor))
main()