hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_sort_intvec_search (m01nb)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_sort_intvec_search (m01nb) searches an ordered vector of integer numbers and returns the index of the first value equal to the sought-after item.

Syntax

[result, ifail] = m01nb(valid, iv, item, 'm1', m1, 'm2', m2)
[result, ifail] = nag_sort_intvec_search(valid, iv, item, 'm1', m1, 'm2', m2)

Description

nag_sort_intvec_search (m01nb) 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, 0 is returned. Second, if a value equal to the sought-after item is not found, the index of the immediate lower value is returned.

References

Wirth N (2004) Algorithms and Data Structures 35–36 Prentice Hall

Parameters

Compulsory Input Parameters

1:     valid – logical scalar
If valid is set to true argument checking will be performed. If valid is set to false nag_sort_intvec_search (m01nb) will be called without argument checking (which includes checking that array iv is sorted in ascending order) and the function will return with ifail=0. See Further Comments for further details.
2:     ivm2 int64int32nag_int array
Elements m1 to m2 contain integer values to be searched.
Constraint: elements m1 to m2 of iv must be sorted in ascending order.
3:     item int64int32nag_int scalar
The sought-after item.

Optional Input Parameters

1:     m1 int64int32nag_int scalar
Default: 1
The index of the first element of iv to be searched.
Constraint: m11.
2:     m2 int64int32nag_int scalar
Default: the dimension of the array iv.
The index of the last element of iv to be searched.
Constraint: m2m1.

Output Parameters

1:     result int64int32nag_int scalar
The result of the function.
2:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
(Note:  these errors will only be returned if valid=true.)
   ifail=2
On entry, iv must be sorted in ascending order.
   ifail=3
Constraint: m11.
   ifail=4
Constraint: m1m2.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

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 iv is in fact arranged in ascending order. If you wish to search the same array iv many times, you are recommended to set valid to true on first call of nag_sort_intvec_search (m01nb) and to false on subsequent calls, in order to minimize the amount of time spent checking iv, which may be significant if iv is large.
The time taken by nag_sort_intvec_search (m01nb) is Ologn, where n=m2-m1+1, when valid=false.

Example

This example reads a list of integer numbers and sought-after items and performs the search for these items.
function m01nb_example


fprintf('m01nb example results\n\n');

iv   = [int64( 5)  6  11  12  13  13  21  23 ...
                23  41  58  59  65  65  86  99];
item = [int64(21)  4  71 100];

m1   = [int64( 1)  1   1   5];
m2   = [int64(10) 16  16  16];

validate = true;
for j = 1:4
  [result, ifail] = m01nb( ...
                           validate, iv, item(j), 'm1', m1(j), 'm2', m2(j));
  validate = false;

  fprintf('Search for %5d in index range [%2d:%2d]: index = %2d\n', ...
          item(j), m1(j), m2(j), result);
end


m01nb example results

Search for    21 in index range [ 1:10]: index =  7
Search for     4 in index range [ 1:16]: index =  0
Search for    71 in index range [ 1:16]: index = 14
Search for   100 in index range [ 5:16]: index = 16

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015