function binarySearch(a, value, left, right)
if right < left
return not found
mid := floor((left+right)/2)
if value > a[mid]
return binarySearch(a, value, mid+1, right)
else if value < a[mid]
return binarySearch(a, value, left, mid-1)
else
return mid
Because the calls are tail-recursive, this can be rewritten as a loop, making the algorithm in-place:
function binarySearch(a, value, left, right)
while left <= right
mid := floor((left+right)/2)
if value > a[mid]
left := mid+1
else if value < a[mid]
right := mid-1
else
return mid
return not found