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_rank (m01da)

## Purpose

nag_sort_realvec_rank (m01da) ranks a vector of double numbers in ascending or descending order.

## Syntax

[irank, ifail] = m01da(rv, m1, order, 'm2', m2)
[irank, ifail] = nag_sort_realvec_rank(rv, m1, order, 'm2', m2)

## Description

nag_sort_realvec_rank (m01da) uses a variant of list-merging, as described on pages 165–166 in Knuth (1973). The function takes advantage of natural ordering in the data, and uses a simple list insertion in a preparatory pass to generate ordered lists of length at least 10$10$. The ranking is stable: equal elements preserve their ordering in the input data.

## References

Knuth D E (1973) The Art of Computer Programming (Volume 3) (2nd Edition) Addison–Wesley

## Parameters

### Compulsory Input Parameters

1:     rv(m2) – double array
m2, the dimension of the array, must satisfy the constraint m2m1${\mathbf{m2}}\ge {\mathbf{m1}}$.
Elements m1 to m2 of rv must contain double values to be ranked.
2:     m1 – int64int32nag_int scalar
The index of the first element of rv to be ranked.
Constraint: m1 > 0${\mathbf{m1}}>0$.
3:     order – string (length ≥ 1)
If order = 'A'${\mathbf{order}}=\text{'A'}$, the values will be ranked in ascending (i.e., nondecreasing) order.
If order = 'D'${\mathbf{order}}=\text{'D'}$, into descending order.
Constraint: order = 'A'${\mathbf{order}}=\text{'A'}$ or 'D'$\text{'D'}$.

### Optional Input Parameters

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

None.

### Output Parameters

1:     irank(m2) – int64int32nag_int array
Elements m1 to m2 of irank contain the ranks of the corresponding elements of rv. Note that the ranks are in the range m1 to m2: thus, if rv(i)${\mathbf{rv}}\left(i\right)$ is the first element in the rank order, irank(i)${\mathbf{irank}}\left(i\right)$ is set to m1.
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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, m2 < 1${\mathbf{m2}}<1$, or m1 < 1${\mathbf{m1}}<1$, or m1 > m2${\mathbf{m1}}>{\mathbf{m2}}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, order is not 'A' or 'D'.

## Accuracy

Not applicable.

The average time taken by the function is approximately proportional to n × log(n)$n×\mathrm{log}\left(n\right)$, where n = m2m1 + 1$n={\mathbf{m2}}-{\mathbf{m1}}+1$.

## Example

```function nag_sort_realvec_rank_example
rv = [5.3;
4.6;
7.8;
1.7;
5.3;
9.9;
3.2;
4.3;
7.8;
4.5;
1.2;
7.6];
m1 = int64(1);
order = 'Ascending';
[irank, ifail] = nag_sort_realvec_rank(rv, m1, order)
```
```

irank =

7
6
10
2
8
12
3
4
11
5
1
9

ifail =

0

```
```function m01da_example
rv = [5.3;
4.6;
7.8;
1.7;
5.3;
9.9;
3.2;
4.3;
7.8;
4.5;
1.2;
7.6];
m1 = int64(1);
order = 'Ascending';
[irank, ifail] = m01da(rv, m1, order)
```
```

irank =

7
6
10
2
8
12
3
4
11
5
1
9

ifail =

0

```