NAG Library Function Document
nag_search_char (m01ncc) 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.
||nag_search_char (Nag_Boolean validate,
const char *ch,
const char *item,
nag_search_char (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,
is returned. Second, if a value equal to the sought-after item is not found, the index of the immediate lower value is returned.
Wirth N (2004) Algorithms and Data Structures 35–36 Prentice Hall
validate – Nag_BooleanInput
: if validate
is set to Nag_TRUE argument checking will be performed. If validate
is set to Nag_FALSE nag_search_char (m01ncc) will be called without argument checking, which includes checking that array ch
is sorted in ascending order and the function will return with
NE_NOERROR. See Section 8
for further details.
ch – const char *Input
: elements m1
contain null terminated strings to be searched.
must be sorted in ascending order. The length of each element of ch
must not exceed
. Trailing space characters are ignored.
m1 – IntegerInput
: the index of the first element of
to be searched.
m2 – IntegerInput
the index of the last element of
to be searched.
item – const char *Input
On entry: the sought-after item. Trailing space characters are ignored.
fail – NagError *Input/Output
The NAG error argument (see Section 3.6
in the Essential Introduction).
6 Error Indicators and Warnings
Dynamic memory allocation failed.
On entry, argument had an illegal value.
On entry, the length of each element of ch
must be at most
maximum string length
On entry, .
On entry, and .
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
On entry, ch
must be sorted in ascending order:
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 nag_search_char (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
The time taken by nag_search_char (m01ncc) is , where , when .
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 (m01ncce.c)
9.2 Program Data
Program Data (m01ncce.d)
9.3 Program Results
Program Results (m01ncce.r)