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_charvec_search (m01nc)

Purpose

nag_sort_charvec_search (m01nc) examines an ordered vector of null terminated strings and returns the index of the first value equal to the sought-after item. Character items are compared according to the ASCII collating sequence.

Syntax

[result, ifail] = m01nc(valid, ch, item, 'm1', m1, 'm2', m2)
[result, ifail] = nag_sort_charvec_search(valid, ch, item, 'm1', m1, 'm2', m2)

Description

nag_sort_charvec_search (m01nc) 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, 00 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 parameter checking will be performed. If valid is set to false nag_sort_charvec_search (m01nc) will be called without parameter checking, which includes checking that array ch is sorted in ascending order and the function will return with ifail = 0ifail=0. See Section [Further Comments] for further details.
2:     ch(m2) – cell array of strings
m2, the dimension of the array, must satisfy the constraint m2m1m2m1.
Elements m1 to m2 contain character data to be searched.
Constraint: elements m1 to m2 of ch must be sorted in ascending order. The length of each element of ch must not exceed 255255. Trailing space characters are ignored.
3:     item – string
The sought-after item. Trailing space characters are ignored.

Optional Input Parameters

1:     m1 – int64int32nag_int scalar
Default: 11
The index of the first element of ch to be searched.
Constraint: m11m11.
2:     m2 – int64int32nag_int scalar
Default: The dimension of the array ch.
The index of the last element of ch to be searched.
Constraint: m2m1m2m1.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     result – int64int32nag_int scalar
The result of the function.
2:     ifail – int64int32nag_int scalar
ifail = 0ifail=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 = truevalid=true.)
  ifail = 2ifail=2
On entry, elements m1 to m2 of ch are not sorted in ASCII order.
  ifail = 3ifail=3
On entry, m2 < 1m2<1.
  ifail = 4ifail=4
On entry, m1 > m2m1>m2.
  ifail = 5ifail=5
On entry, the length of elements of ch exceeds 255255.

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

Example

function nag_sort_charvec_search_example
ch = {'blue  ', 'green', 'red', 'yellow'};
result = zeros(4, 1, 'int64');
validate = true;
item = 'blue';
[result(1), ifail] = nag_sort_charvec_search(validate, ch, item);
validate = false;
item = 'azure';
[result(2), ifail] = nag_sort_charvec_search(validate, ch, item);
item = 'yellow';
[result(3), ifail] = nag_sort_charvec_search(validate, ch, item);
item = 'red';
[result(4), ifail] = nag_sort_charvec_search(validate, ch, item)
 

result =

                    1
                    0
                    4
                    3


ifail =

                    0


function m01nc_example
ch = {'a02aa'; 'a02ab'; 'a02ac'; 'c02ad'; 'c02ae'; 'c05ad'; 'c05ag'; 'c05aj'; 'c05ax'; 'c05az'};
result = zeros(4, 1, 'int64');
validate = true;
item = 'c02ad';
[result(1), ifail] = m01nc(validate, ch, item);
validate = false;
item = 'a01aa';
[result(2), ifail] = m01nc(validate, ch, item);
item = 'c04ay';
[result(3), ifail] = m01nc(validate, ch, item);
item = 'c05az';
[result(4), ifail] = m01nc(validate, ch, item)
 

result =

                    4
                    0
                    5
                   10


ifail =

                    0



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–2013