Binary Search


StringListToSearch() is the sorted array you want to search. stringListToSearch should be sorted, smallest to largest, for this to work.

StringListToSearchFor(?) is the value you want to find.

Min is the start point of where you want to search in the array.

Max is the end point.

Existsinarray returns -1 if the value was not found, and the index location of the value otherwise.


function existsInArray(stringListToSearch() as string, stringToSearchFor as string, min as integer, max as integer) as integer
dim n as integer
do
if min > max then exit do
n = (min+max) / 2
if stringToSearchFor = stringListToSearch[n] then existsInArray = n: exit function
if stringToSearchFor < stringListToSearch[n] then
max = n - 1
else
min = n + 1
end if
loop
existsInArray = -1
end function

-Agamemnus


BinarySearch - page last edited 2010-01-28 20:22:50 by 75.67.131.67 (home) (edit)
Blast WIKI - by RoboticBoy - edited and tweaked for our evil purposes by Hexadecimal Disaster