Search Problem
Find the index of element
Theory
Recursively divide the array into two halves;
Implementation
def binary_search(arr, x):
lo, hi = 0, len(arr) - 1
while lo <= hi:
mid = int((lo + hi) / 2)
if arr[mid] == x:
return mid
lo = mid + 1 if x > arr[mid] else lo
hi = mid - 1 if x < arr[mid] else hi
return lo
Runtime
Binary search divides the array of size