RosettaCodeData/Task/Binary-search/PL-I/binary-search.pli

17 lines
414 B
Text
Raw Permalink Normal View History

2013-04-10 22:43:41 -07:00
/* A binary search of list A for element M */
search: procedure (A, M) returns (fixed binary);
declare (A(*), M) fixed binary;
declare (l, r, mid) fixed binary;
l = lbound(a,1)-1; r = hbound(A,1)+1;
2013-06-05 21:47:54 +00:00
do while (l <= r);
2013-04-10 22:43:41 -07:00
mid = (l+r)/2;
if A(mid) = M then return (mid);
if A(mid) < M then
2013-06-05 21:47:54 +00:00
L = mid+1;
2013-04-10 22:43:41 -07:00
else
2013-06-05 21:47:54 +00:00
R = mid-1;
2013-04-10 22:43:41 -07:00
end;
return (lbound(A,1)-1);
end search;