RosettaCodeData/Task/Binary-search/MATLAB/binary-search-3.m

21 lines
342 B
Mathematica
Raw Permalink Normal View History

2013-04-10 15:42:53 -07:00
function mid = binarySearchIter(list,value)
2013-06-05 21:47:54 +00:00
low = 1;
2013-04-10 15:42:53 -07:00
high = numel(list) - 1;
while( low <= high )
mid = floor((low + high)/2);
if( list(mid) > value )
high = mid - 1;
elseif( list(mid) < value )
low = mid + 1;
else
return
end
end
mid = [];
end