Binary search: Difference between revisions

(→‎Iterative, exclusive bounds, three-way test.: Perhaps one should write this sort of routine in assembler...)
Line 1,420:
REAL X,A(*) !Where is X in array A(1:N)?
INTEGER N !The count.
INTEGER I,L,R,P !Fingers.
L = 0 !Establish outer bounds, to search A(L+1:R-1).
R = N + 1 !L = first - 1; R = last + 1.
Line 1,444:
REAL X,A(*) !Where is X in array A(1:N)?
INTEGER N !The count.
INTEGER I,L,R,P !Fingers.
R = N + 1 !Establish outer bounds, to search A(L+1:R-1).
P = 0 !L = first - 1; R = last + 1.
Line 1,471:
if X > 0 then print "Positive"
else if X > 0 then print "Still positive";
That is, does the compiler make any remark, and does the resulting machine code contain a redundant test? However, despite all the above, the three-way IF statement has been declared deprecated in later versions of Fortran, with no alternative to repeated testing offered.
 
Incidentally, the exclusive-bounds version leads to a good version of the interpolation search (whereby the probe position is interpolated, not just in the middle of the span), unlike the version based on inclusive-bounds. Further, the unsourced offering in Wikipedia contains a bug - try searching an array of two equal elements.
 
=={{header|GAP}}==
1,220

edits