-
Notifications
You must be signed in to change notification settings - Fork 0
/
q9.py
34 lines (31 loc) · 969 Bytes
/
q9.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
def iterative_binary(arr, low, high, x):
while (low<=high):
mid = low + (high-low)//2
if arr[mid]==x:
return mid
elif arr[mid]<x:
low = mid + 1
else:
high = mid - 1
return -1
def recursive_binary(arr, low, high, x):
if high>=low:
mid = low + (high-low)//2
if arr[mid]==x:
return mid
elif arr[mid]<x:
return recursive_binary(arr,mid+1,high,x)
else:
return recursive_binary(arr,low,mid-1,x)
return -1
if __name__=='__main__':
arr = [20,13,24,54,41,57,34,56,]
arr = sorted(arr)
print(arr)
x = int(input("enter value to search : "))
pos = iterative_binary(arr,0,len(arr),x)
pos2 = recursive_binary(arr,0,len(arr),x)
if (pos != -1 and pos2 != -1):
print(f"{x} is present at index {pos}")
else:
print("element not found")