nag_sort_realvec_search (m01na) is based on Professor Niklaus Wirth's implementation of the Binary Search algorithm (see
Wirth (2004)), but with two modifications. First, if the sought-after item is less than the value of the first element of the array to be searched,
is returned. Second, if a value equal to the sought-after item is not found, the index of the immediate lower value is returned.
Not applicable.
The argument
valid should be used with caution. Set it to
false only if you are confident that the other arguments are correct, in particular that array
rv is in fact arranged in ascending order. If you wish to search the same array
rv many times, you are recommended to set
valid to
true on first call of
nag_sort_realvec_search (m01na) and to
false on subsequent calls, in order to minimize the amount of time spent checking
rv, which may be significant if
rv is large.
This example reads a list of double precision numbers and sought-after items and performs the search for these items.
function m01na_example
fprintf('m01na example results\n\n');
rv = [0.5 0.6 1.1 1.2 1.3 1.3 2.1 2.3 ...
2.3 4.1 5.8 5.9 6.5 6.5 8.6 9.9];
item = [ 2.1 0.4 7.1 10];
m1 = int64([ 1 1 1 5]);
m2 = int64([10 16 16 16]);
validate = true;
for j = 1:4
[result, ifail] = m01na( ...
validate, rv, item(j), 'm1', m1(j), 'm2', m2(j));
validate = false;
fprintf('Search for %7.1f in index range [%2d:%2d]: index = %2d\n', ...
item(j), m1(j), m2(j), result);
end