Currently, when I search for the element that is at the midpoint it returns the correct index, but for any other element it does not work for me.
I think I am making a mistake when I split the array:
aList = [1,3,5,6,8,9,10,12,34,56,78,456]
def recursiveBinarySearch(aList, target):
#aList = sorted(aList)
if len(aList) == 0:
return False
else:
midpoint = len(aList) // 2
if aList[midpoint] == target:
return aList.index(target)
else:
if target < aList[midpoint]:
return recursiveBinarySearch(aList[:midpoint],target)
else:
return recursiveBinarySearch(aList[midpoint+1:],target)
print(recursiveBinarySearch(aList,9))