Regarding the binary search implementation given below:
def bin_search(arr, key):
n = len(arr)
if n < 2:
return (0 if (n == 1 and arr[0] == key) else None)
m = int(0.5 * n)
if arr[m] > key:
return bin_search(arr[:m], key)
result = bin_search(arr[m:], key)
return (result + m if result != None else None)
For the above binary search implementation, time complexity will be affected as we are taking slice of an array and space complexity too as list slicing in python creates a new list object. For improving the above implementation, I am thinking of introducing lower and upper bound variables just as in its original implementation. But it will modify the above code implementation completely.
Can you please let me know how to modify the above implementation so that the time and space complexity of it is improved and is my understanding regarding its complexity correct?