nag_search_double (m01nac) (PDF version)
m01 Chapter Contents
m01 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_search_double (m01nac)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

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

2  Specification

#include <nag.h>
#include <nagm01.h>
Integer  nag_search_double (Nag_Boolean validate, const double rv[], Integer m1, Integer m2, double item, NagError *fail)

3  Description

nag_search_double (m01nac) 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_double (m01nac) will be called without argument checking (which includes checking that array rv is sorted in ascending order) and the function will return with fail.code= NE_NOERROR. See Section 9 for further details.
2:     rv[m2+1]const doubleInput
On entry: elements m1 to m2 contain double values to be searched.
Constraint: elements m1 to m2 of rv must be sorted in ascending order.
3:     m1IntegerInput
On entry: the index of the first element of rv to be searched.
Constraint: m10.
4:     m2IntegerInput
On entry: the index of the last element of rv to be searched.
Constraint: m2m1.
5:     itemdoubleInput
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

NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_INT
On entry, m1=value.
Constraint: m10.
NE_INT_2
On entry, m1=value, m2=value.
Constraint: m1m2.
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, rv must be sorted in ascending order: rv​ element ​value>​ element ​value.

7  Accuracy

Not applicable.

8  Parallelism and Performance

Not applicable.

9  Further Comments

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 rv is in fact arranged in ascending order. If you wish to search the same array rv many times, you are recommended to set validate to Nag_TRUE on first call of nag_search_double (m01nac) and to Nag_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_search_double (m01nac) is Ologn, where n=m2-m1+1, when validate=Nag_FALSE.

10  Example

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

10.1  Program Text

Program Text (m01nace.c)

10.2  Program Data

Program Data (m01nace.d)

10.3  Program Results

Program Results (m01nace.r)


nag_search_double (m01nac) (PDF version)
m01 Chapter Contents
m01 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2014