08-19-2003, 05:11 AM
I guess it's time I posted my old code for doing a binary search.*****
Code:
REM "BINSER" BINARY SEARCH SUBROUTINE. Edward F. Moneo 02-Jun-89.
REM
REM CALLING SEQUENCE:
REM BSTAB() = Name of the table to be searched.
REM Z = Number of entries in table.
REM BSARG = Search argument.
REM
REM ON OUTPUT: Z = Found table index, (Z=0=Not found)
BINSER:
IF Z=0 THEN RETURN
ZTOP=0 'Pointer to the top -1
ZBOT=Z+1 'Pointer to the bottom+1
IF Z=1 THEN 'Set increment (table offset)
ZINC=1
ELSE
ZINC=Z/2
END IF
DO
Z=ZTOP+ZINC
IF BSTAB(Z)=BSARG THEN RETURN
IF BSTAB(Z) < BSARG THEN
ZTOP=ZTOP+ZINC
ELSE
ZBOT=ZTOP+ZINC
END IF
ZINC=ZINC/2
IF ZINC=0 THEN ZINC=1
LOOP WHILE ZTOP+ZINC < ZBOT
Z=0
RETURN