Is this binary search of the sorted list going to be faster if we pass list slice instead of the whole list, where the list has millions of items?
Normal:
def binary_search(data, target, low, high):
if low > high:
return False
else:
mid = (low + high) // 2
if target == data[mid]:
return True
elif target < data[mid]:
return binary_search(data, target, low, mid-1)
else:
return binary_search(data, target, mid+1, high)
With list slice(I had to modify it a bit):
def binary_search(data, target, low, high):
if low > high:
return False
else:
mid = (low + high) // 2
if target == data[mid]:
return True
elif target < data[mid]:
return binary_search(data[low:mid-1], target, 0, mid)
else:
return binary_search(data[mid+1:high], target, 0, high-mid)
I am currently learning about algorithms so i really don't know if this is the best practice or not.