I am trying to count the number of searches being performed with binary search.
def binarySearch(l, value, low = 0, high = -1):
if not l:
return -1
if(high == -1):
high = len(l)-1
if low == high:
if l[low] == value:
return low
else:
return -1
mid = (low+high)//2
if l[mid] > value:
return binarySearch(l, value, low, mid-1)
elif l[mid] < value:
return binarySearch(l, value, mid+1, high)
else:
return mid
I had a friend suggest using a for loop and counting the number of iterations, but therein lies my confusion. I am uncertain of the parameters by which to initialize the for loop to perform said task.
Any thoughts would be greatly appreciated. Thanks.