Binary Searching Text Files

Date:    Fri, 13 Jan 1995 10:31:52 GMT
From:    Niall Sclater <nls@LANG.GLA.AC.UK>
I haven't followed all the mailings on this subject but if you want to do fast sorting on external text files it is extremely quick to do a binary chop sort using lingo after reading the file into a variable. This is assuming the file is already sorted alphabetically with one entry per line.

I found that reading entries into a list was far too slow and that this method made searching through a large file practicable.

If you're not already familiar with binary searches they go something like this (in psuedo-Lingo), where x is the string you are looking for:

repeat while (not found) and bottom line >= top line
  find the mid point ((the bottom line of the variable - the top line of the variable) / 2)
  if x = the mid point then
    hey presto
  else if x < the mid point then
    set the bottom line to the previous mid point - 1
  else if x > the mid point then
    set the top line to the previous mid point + 1
  end if
end repeat