-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathbisection_recur.py
More file actions
31 lines (23 loc) · 775 Bytes
/
bisection_recur.py
File metadata and controls
31 lines (23 loc) · 775 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
def bisection_recur(n, arr, start, stop):
if start > stop:
return f"{n} not found in list"
else:
mid = (start+stop)//2
if n == arr[mid]:
return f"{n} is found at index {mid}"
elif n > arr[mid]:
start = mid+1
return bisection_recur(n, arr, start, stop)
else:
stop = mid-1
return bisection_recur(n, arr, start, stop)
def create_list(max_val):
arr = []
for num in range(1, max_val+1):
arr.append(num)
return arr
max = int(input("Enter the maximum length of the list : "))
num_to_search = int(input("Enter the number you want to search for : "))
l = create_list(max)
print(l)
print(bisection_recur(num_to_search, l, 1, len(l)-1))