M01 Chapter Contents
M01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentM01NCF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

M01NCF 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.

## 2  Specification

 FUNCTION M01NCF ( VALID, CH, M1, M2, ITEM, IFAIL)
 INTEGER M01NCF
 INTEGER M1, M2, IFAIL LOGICAL VALID CHARACTER(*) CH(M2), ITEM

## 3  Description

M01NCF 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.

## 4  References

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

## 5  Parameters

1:     VALID – LOGICALInput
On entry: if VALID is set to .TRUE. parameter checking will be performed. If VALID is set to .FALSE. M01NCF will be called without parameter checking, which includes checking that array CH is sorted in ascending order and the routine will return with ${\mathbf{IFAIL}}={\mathbf{0}}$. See Section 8 for further details.
2:     CH(M2) – CHARACTER(*) arrayInput
On entry: 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 $255$. Trailing space characters are ignored.
3:     M1 – INTEGERInput
On entry: the index of the first element of CH to be searched.
Constraint: ${\mathbf{M1}}\ge 1$.
4:     M2 – INTEGERInput
On entry: the index of the last element of CH to be searched.
Constraint: ${\mathbf{M2}}\ge {\mathbf{M1}}$.
5:     ITEM – CHARACTER(*)Input
On entry: the sought-after item. Trailing space characters are ignored.
6:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
(Note:  these errors will only be returned if ${\mathbf{VALID}}=\mathrm{.TRUE.}$.)
${\mathbf{IFAIL}}=2$
On entry, elements M1 to M2 of CH are not sorted in ASCII order.
${\mathbf{IFAIL}}=3$
On entry, ${\mathbf{M2}}<1$.
${\mathbf{IFAIL}}=4$
On entry, ${\mathbf{M1}}>{\mathbf{M2}}$.
${\mathbf{IFAIL}}=5$
On entry, the length of elements of CH exceeds $255$.

## 7  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 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 M01NCF 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 M01NCF is $\mathit{O}\left(\mathrm{log}n\right)$, where $n={\mathbf{M2}}-{\mathbf{M1}}+1$, when ${\mathbf{VALID}}=\mathrm{.FALSE.}$.

## 9  Example

This example reads a list of character data and sought-after items and performs the search for these items.

### 9.1  Program Text

Program Text (m01ncfe.f90)

### 9.2  Program Data

Program Data (m01ncfe.d)

### 9.3  Program Results

Program Results (m01ncfe.r)