home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD-ROM Magazine 28 Bonus
/
CDRomMagazine-SoftKey-ArtPassion-FrenchVersion-Win31Mac.bin
/
data
/
shared.dir
/
03056_Script_BinarySearch
< prev
next >
Wrap
Text File
|
1996-06-21
|
1KB
|
44 lines
on BinSearchFirstItemInLine source, target
-- Source must be sorted, and have a blank line at the end.
if (length(source) = 0) or (length(target) = 0) then return 0
set RangeBegin = 1
set RangeEnd = the number of lines of source
set CurrentLine = RangeBegin + integer ((RangeEnd - RangeBegin) / 2)
set loopCount = 0 -- For error handling, to avoid infinite loop
set found = FALSE
repeat while found = FALSE
set maybeTarget = item 1 of line CurrentLine of Source
-- in case the line begins with a ":"
if maybeTarget = EMPTY then
set currentline = currentline - 1
else if maybeTarget = Target then
set found = TRUE
else if maybeTarget < Target then
set RangeBegin = CurrentLine
set CurrentLine = RangeBegin + integer ((RangeEnd - RangeBegin) / 2)
else -- maybeTarget > Target
set RangeEnd = CurrentLine
set CurrentLine = RangeBegin + integer ((RangeEnd - RangeBegin) / 2)
end if
-------------------------------------------------------------------------
-- Error Handling. If data is not found, avoid an infinite loop.
if abs(RangeEnd - RangeBegin) < 2 then set loopCount = loopCount + 1
if loopCount > 3 then
exit repeat
end if
-------------------------------------------------------------------------
end repeat
if found then return CurrentLine
else return 0
end