RosettaCodeData/Task/Binary-search/BBC-BASIC/binary-search.basic

24 lines
662 B
Plaintext

DIM array%(9)
array%() = 7, 14, 21, 28, 35, 42, 49, 56, 63, 70
secret% = 42
index% = FNwhere(array%(), secret%, 0, DIM(array%(),1))
IF index% >= 0 THEN
PRINT "The value "; secret% " was found at index "; index%
ELSE
PRINT "The value "; secret% " was not found"
ENDIF
END
REM Search ordered array A%() for the value S% from index B% to T%
DEF FNwhere(A%(), S%, B%, T%)
LOCAL H%
H% = 2
WHILE H%<(T%-B%) H% *= 2:ENDWHILE
H% /= 2
REPEAT
IF (B%+H%)<=T% IF S%>=A%(B%+H%) B% += H%
H% /= 2
UNTIL H%=0
IF S%=A%(B%) THEN = B% ELSE = -1