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
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
min = n + 1
end if
existsInArray = -1
end function


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