-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.py
More file actions
28 lines (19 loc) · 839 Bytes
/
Copy pathbinary_search.py
File metadata and controls
28 lines (19 loc) · 839 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
import random
def binary_search(sorted_data, target, low_index, high_index):
if low_index > high_index:
return False
mid_index = (low_index + high_index) // 2
if target == sorted_data[mid_index]:
return (True, mid_index)
elif target < sorted_data[mid_index]:
return binary_search(sorted_data, target, low_index, mid_index - 1)
elif target > sorted_data[mid_index]:
return binary_search(sorted_data, target, mid_index + 1, high_index)
if __name__ == "__main__":
data = [random.randint(0,100) for i in range(10)]
print(f'Data: {data}')
sorted_data = sorted(data)
print(f'Sorted data: {sorted_data}')
target = int(input('What number would you like to find? '))
found = binary_search(sorted_data, target, 0, len(sorted_data) - 1)
print(found)