: Thank you for replying. I think the program stops responding because
: a loop keeps running. I think I am using the goto incorrectly? I
: know the file is being read because I can call specific elements of
: the array and it works. It's just the binary search I'm having
: problems with.
:
I've gone over your program several times and I can't figure out how it is supposed to work. I believe that the problem is that your search algorithm is simply wrong. It's more complex than it needs to be and (sorry) it simply does not work. I think your attempt to use this code has lead to more confusion than enlightenment.
The following makes use of an algorithm in Knuth's THE ART OF COMPUTER PROGRAMMING.
: I don't know what you meant by the comparison between SearchValue
: and StringArray[i].
:
This program assumes that the first three characters in each record make up the "key", i.e., the sample of data that you are searching for. It also assumes that the operator will enter exactly three characters (SearchValue) when prompted, no more, no less. Anything else and the program will not work.
Once the operator has entered SearchValue, those three characters have to be compared with the first three characters of StringArray[i] that is being examined. We extract the first three characters and store them in the string "Key". This is done in the statement
Key := Copy(StringArray[i], 1, 3) ;
SearchValue is then compared to key (twice) in the statement
if SearchValue < Key then
High := i - 1
else if SearchValue > Key then
Low := i + 1
else begin
writeln(StringArray[i]) ;
Halt
end
If each character in SearchValue is the same as the corresponding character in Key then the two are equal; if not then which is greatest is determined by the ascii representation of the first mismatched characters. The statement tests for SearchValue < Key, SearchValue > Key. The only other possibility, SearchValue = Key, becomes the
else part (and signals a successful end of the search).
Program Search;
Var
f : Text ;
i : Integer ;
StringArray : Array[0 .. 20] of String ;
Low, High : Integer ;
SearchValue : String ;
Key : String ;
Begin
{
load file into memory
}
assign(f, 'filename.txt') ;
reset(f) ;
i := 0 ;
while not eof(f) do
begin
if i > 20 then begin
writeln ('File is too big to fit in memory.') ;
close(f) ;
halt
end ;
readln(f, StringArray[i]) ;
i := i + 1
end ;
close(f) ;
write('Enter Person ID to see contact details: ') ;
readln(SearchValue) ;
{
binary search algorithm from
Knuth's THE ART OF COMPUTER PROGRAMMING
Vol 3, 2nd edition, page 410
}
Low := 0 ;
High := i - 1 ;
while High >= Low do begin { High < Low means the search has failed }
{
at this point we know that if SearchValue is in the
array it satisfies
Key[Low] <= SearchValue <= Key[High]
}
i := (Low + High) div 2 ; { get midpoint }
{
Compare
}
Key := Copy(StringArray[i], 1, 3) ;
if SearchValue < Key then
High := i - 1
else if SearchValue > Key then
Low := i + 1
else begin { Success! Search = Key - the only other possibility }
writeln(StringArray[i]) ;
Halt
end
end ;
{
Failure
}
writeln(SearchValue + ' not found!')
End.
I've tested this program using the data I previously posted and it seems to work.
Actor