Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_sort_realvec_search (m01na)

## Purpose

nag_sort_realvec_search (m01na) searches an ordered vector of double numbers and returns the index of the first value equal to the sought-after item.

## Syntax

[result, ifail] = m01na(valid, rv, item, 'm1', m1, 'm2', m2)
[result, ifail] = nag_sort_realvec_search(valid, rv, item, 'm1', m1, 'm2', m2)

## Description

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, 0$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 parameter checking will be performed. If valid is set to false nag_sort_realvec_search (m01na) will be called without parameter checking (which includes checking that array rv is sorted in ascending order) and the function will return with ${\mathbf{ifail}}={\mathbf{0}}$. See Section [Further Comments] for further details.
2:     rv(m2) – double array
m2, the dimension of the array, must satisfy the constraint m2m1${\mathbf{m2}}\ge {\mathbf{m1}}$.
Elements m1 to m2 contain double values to be searched.
Constraint: elements m1 to m2 of rv must be sorted in ascending order.
3:     item – double scalar
The sought-after item.

### Optional Input Parameters

1:     m1 – int64int32nag_int scalar
Default: 1$1$
The index of the first element of rv to be searched.
Constraint: m11${\mathbf{m1}}\ge 1$.
2:     m2 – int64int32nag_int scalar
Default: The dimension of the array rv.
The index of the last element of rv to be searched.
Constraint: m2m1${\mathbf{m2}}\ge {\mathbf{m1}}$.

None.

### Output Parameters

1:     result – int64int32nag_int scalar
The result of the function.
2:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{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${\mathbf{valid}}=\mathbf{true}$.)
ifail = 2${\mathbf{ifail}}=2$
On entry, elements m1 to m2 of rv are not sorted in ascending order.
ifail = 3${\mathbf{ifail}}=3$
On entry, m2 < 1${\mathbf{m2}}<1$.
ifail = 4${\mathbf{ifail}}=4$
On entry, m1 > m2${\mathbf{m1}}>{\mathbf{m2}}$.

## Accuracy

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.
The time taken by nag_sort_realvec_search (m01na) is O(log(n))$\mathit{O}\left(\mathrm{log}\left(n\right)\right)$, where n = m2m1 + 1$n={\mathbf{m2}}-{\mathbf{m1}}+1$, when valid = false${\mathbf{valid}}=\mathbf{false}$.

## Example

```function nag_sort_realvec_search_example
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];
result = zeros(4, 1, 'int64');
validate = true;
item = 2.1;
[result(1), ifail] = nag_sort_realvec_search(validate, rv, item, 'm2', int64(10));
validate = false;
item = 0.4;
[result(2), ifail] = nag_sort_realvec_search(validate, rv, item);
item = 7.1;
[result(3), ifail] = nag_sort_realvec_search(validate, rv, item);
item = 10;
[result(4), ifail] = nag_sort_realvec_search(validate, rv, item, 'm1', int64(5))
```
```

result =

7
0
14
16

ifail =

0

```
```function m01na_example
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];
result = zeros(4, 1, 'int64');
validate = true;
item = 2.1;
[result(1), ifail] = m01na(validate, rv, item, 'm2', int64(10));
validate = false;
item = 0.4;
[result(2), ifail] = m01na(validate, rv, item);
item = 7.1;
[result(3), ifail] = m01na(validate, rv, item);
item = 10;
[result(4), ifail] = m01na(validate, rv, item, 'm1', int64(5))
```
```

result =

7
0
14
16

ifail =

0

```