m01 Chapter Contents
m01 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_search_int (m01nbc)

## 1  Purpose

nag_search_int (m01nbc) searches an ordered vector of integer numbers and returns the index of the first value equal to the sought-after item.

## 2  Specification

 #include #include
 Integer nag_search_int (Nag_Boolean validate, const Integer iv[], Integer m1, Integer m2, Integer item, NagError *fail)

## 3  Description

nag_search_int (m01nbc) 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.

## 4  References

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

## 5  Arguments

1:     validateNag_BooleanInput
On entry: if validate is set to Nag_TRUE argument checking will be performed. If validate is set to Nag_FALSE nag_search_int (m01nbc) will be called without argument checking (which includes checking that array iv is sorted in ascending order) and the function will return with NE_NOERROR. See Section 8 for further details.
2:     iv[${\mathbf{m2}}+1$]const IntegerInput
On entry: elements m1 to m2 contain integer values to be searched.
Constraint: elements m1 to m2 of iv must be sorted in ascending order.
3:     m1IntegerInput
On entry: the index of the first element of iv to be searched.
Constraint: ${\mathbf{m1}}\ge 0$.
4:     m2IntegerInput
On entry: the index of the last element of iv to be searched.
Constraint: ${\mathbf{m2}}\ge {\mathbf{m1}}$.
5:     itemIntegerInput
On entry: the sought-after item.
6:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{m1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m1}}\ge 0$.
NE_INT_2
On entry, ${\mathbf{m1}}=〈\mathit{\text{value}}〉$, ${\mathbf{m2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m1}}\le {\mathbf{m2}}$.
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.
NE_NOT_INCREASING
On entry, iv must be sorted in ascending order: ${\mathbf{iv}}\text{​ element ​}〈\mathit{\text{value}}〉>\text{​ element ​}〈\mathit{\text{value}}〉$.

## 7  Accuracy

Not applicable.

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 iv is in fact arranged in ascending order. If you wish to search the same array iv many times, you are recommended to set validate to Nag_TRUE on first call of nag_search_int (m01nbc) and to Nag_FALSE on subsequent calls, in order to minimize the amount of time spent checking iv, which may be significant if iv is large.
The time taken by nag_search_int (m01nbc) is $\mathit{O}\left(\mathrm{log}n\right)$, where $n={\mathbf{m2}}-{\mathbf{m1}}+1$, when ${\mathbf{validate}}=\mathrm{Nag_FALSE}$.

## 9  Example

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

### 9.1  Program Text

Program Text (m01nbce.c)

### 9.2  Program Data

Program Data (m01nbce.d)

### 9.3  Program Results

Program Results (m01nbce.r)