Write a Recursive function in python BinarySearch(Arr, L, R, X) to search the given element X to be searched from the List Arr having R elements, where L represents lower bound and R represents the upper bound.
Source Code
def binarySearch (Arr, L, R, X):
if R >= L:
mid = L + (R - L)//2
if Arr[mid] == X:
return mid
elif Arr[mid] > X:
return binarySearch(Arr, L, mid-1, X)
else:
return binarySearch(Arr, mid+1, R, X)
else:
return -1
#calling the function
data = [10,20,30,40,50,60,70,80,90,100]
key = 90
index = binarySearch(data,0,len(data)-1,key)
if(index!=-1):
print('Found at position :',index+1)
else:
print('Not found')
Output