def binary_search(lst, n, ln):
start = 0
end = ln - 1
mid = 0
while start <= end:
mid = (end + start) // 2
if lst[mid] < n:
start = mid + 1
elif lst[mid] > n:
end = mid - 1
else:
return mid
return -1
lst = [1, 2, 3, 7, 9, 11, 17, 20, 40]
result = binary_search(lst, 9, len(lst))
if result == -1:
print("Element not found")
else:
print("Element found at index", result)
'''
run:
Element found at index 4
'''