# NAG CL Interfacem01ncc (charvec_​search)

## 1Purpose

m01ncc examines an ordered vector of 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.

## 2Specification

 #include
 Integer m01ncc (Nag_Boolean validate, const char *ch[], Integer m1, Integer m2, const char *item, NagError *fail)
The function may be called by the names: m01ncc, nag_sort_charvec_search or nag_search_char.

## 3Description

m01ncc 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, $-1$ is returned. Second, if a value equal to the sought-after item is not found, the index of the immediate lower value is returned.

## 4References

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

## 5Arguments

1: $\mathbf{validate}$Nag_Boolean Input
On entry: if validate is set to Nag_TRUE argument checking will be performed. If validate is set to Nag_FALSE m01ncc will be called without argument checking, which includes checking that array ch is sorted in ascending order and the function will return with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_NOERROR. See Section 9 for further details.
2: $\mathbf{ch}\left[{\mathbf{m2}}+1\right]$const char * Input
On entry: elements m1 to m2 contain null terminated strings 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: $\mathbf{m1}$Integer Input
On entry: the index of the first element of ch to be searched.
Constraint: ${\mathbf{m1}}\ge 0$.
4: $\mathbf{m2}$Integer Input
On entry: the index of the last element of ch to be searched.
Constraint: ${\mathbf{m2}}\ge {\mathbf{m1}}$.
5: $\mathbf{item}$const char * Input
On entry: the sought-after item. Trailing space characters are ignored.
6: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_CHAR_LEN_INVALID
On entry, the length of each element of ch must be at most $255$: maximum string length $\text{}=〈\mathit{\text{value}}〉$.
NE_INT
On entry, ${\mathbf{m1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m1}}\ge 0$.
On entry, ${\mathbf{m1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m1}}\ge 1-1$.
NE_INT_2
On entry, ${\mathbf{m1}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m2}}\ge {\mathbf{m1}}$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_NOT_INCREASING
On entry, ch must be sorted in ascending order: ${\mathbf{ch}}\text{​ element ​}〈\mathit{\text{value}}〉>\text{​ element ​}〈\mathit{\text{value}}〉$.

Not applicable.

## 8Parallelism and Performance

m01ncc is not threaded in any implementation.

The argument validate should be used with caution. Set it to Nag_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 validate to Nag_TRUE on first call of m01ncc and to Nag_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 m01ncc is $\mathit{O}\left(\mathrm{log}\left(n\right)\right)$, where $n={\mathbf{m2}}-{\mathbf{m1}}+1$, when ${\mathbf{validate}}=\mathrm{Nag_FALSE}$.

## 10Example

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

### 10.1Program Text

Program Text (m01ncce.c)

### 10.2Program Data

Program Data (m01ncce.d)

### 10.3Program Results

Program Results (m01ncce.r)