I am trying to understand this function with little to no avail. I completely understand what a binary search is but am only new to the concept of recursion but do have a slight grasp on it. I don't really understand what the default values of low and high would be when first calling the function. As of right now I am just including the search space I know the number is in, but what if I don't or I am not sure of the list length? Otherwise, I understand the recursion process going on here as well as the need for low and high being arguments. The function below is provided in the notes by an online course I am taking; however, it wasn't explained in the lecture and contains no docstrings or references about it.
def bSearch(L, e, low, high):
if high - low < 2:
return L[low] == e or L[high] == e
mid = low + int((high-low)/2)
if L[mid] == e:
return True
if L[mid] > e:
return bSearch(L, e, low, mid-1)
else:
return bSearch(L, e, mid+1, high)
L = [1,3,6,15,34,84,78,256]
print bSearch(L, 15, 4, 8)
print bSearch(L, 84, 0, 6)
Output:
False
True