RosettaCodeData/Task/Binary-search/FreeBASIC/binary-search.basic

13 lines
489 B
Plaintext

function binsearch( array() as integer, target as integer ) as integer
'returns the index of the target number, or -1 if it is not in the array
dim as uinteger lo = lbound(array), hi = ubound(array), md = (lo + hi)\2
if array(lo) = target then return lo
if array(hi) = target then return hi
while lo + 1 < hi
if array(md) = target then return md
if array(md)<target then lo = md else hi = md
md = (lo + hi)\2
wend
return -1
end function