I was slightly incorrect with my last code. According to the example posted by Danny on Piazza (which is a really helpful discussion board), I've managed to fix my code.
Here's the updated version:
binSearch(x, A):
b = 0
e = length(A) - 1
while b <= e:
m = (b+e) // 2
if A[m] <= x:
b = m + 1
else:
e = m - 1
return e
I've traced it with quite a few examples and also tried running the code in Java/Python and it satisfies every condition.
Since the assignment still has a few hours before its due, I can't really post how I went about finding my solution.But I'll post a few more posts regarding how I went about actually proving my algorithm.
Here's the updated version:
binSearch(x, A):
b = 0
e = length(A) - 1
while b <= e:
m = (b+e) // 2
if A[m] <= x:
b = m + 1
else:
e = m - 1
return e
I've traced it with quite a few examples and also tried running the code in Java/Python and it satisfies every condition.
Since the assignment still has a few hours before its due, I can't really post how I went about finding my solution.But I'll post a few more posts regarding how I went about actually proving my algorithm.