Python Program to Perform Binary Search using Recursion
Write Python Program to Perform Binary Search using Recursion
# Write Python Program to Perform Binary Search using Recursion
def binary_search(L, key, low, high):
if high >= low:
mid = (low + high) // 2
if key == L[mid]:
return mid
elif key > L[mid]:
return binary_search(L, key, mid + 1, high)
else:
return binary_search(L, key, low, mid - 1)
else:
return -1
L = [11, 22, 33, 44, 55, 66, 77]
key = 44
idx = binary_search(L, key, 0, len(L) - 1)
print(f'In list {L} the key {key} lies on index : {idx} ')
Output:
In list [11, 22, 33, 44, 55, 66, 77] the key 44 lies on index : 3