I wanted to write a simple binary search algorithm just for practice. However, it doesn't seem to work. I have NO IDEA why its not working. If anyone can help me out, I'd really appreciate that.
def binarysearch(arr, k):
if k not in arr:
return -1
n = len(arr)
mdpt = int((n-1)/2)
if arr[mdpt] == k:
return mdpt
elif arr[mdpt] > k:
binarysearch(arr[0:mdpt+1], k)
else:
binarysearch(arr[mdpt:n], k)
arr = [1, 2, 3, 4, 5]
k = 2
print(binarysearch(arr, k))