%
%	Search for an element in the list and return its index.
%

function search(list: array 1..* of string, key: string, N: int): int

%
%	A little more sophsticated search.
%	Binary search.  Find element i in range 1..N such that
%	list(i) = key, if key exists in list.
%
%	If so, return i, otherwise return 0.

    var first := 1
    var last  := N

    loop
	const middle := (first + last) div 2
	if list(middle) >= key then
		last := middle
	elsif list(middle) < key then
		first := middle + 1
	end if
	exit when first >= last
    end loop

    if list(first) = key then
	result first
    else
    	result 0
    end if

% But what if list(middle) = key in the first place (or thereafter?)

end search
