The function may be called by the names: m01nbc, nag_sort_intvec_search or 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, 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
1: – Nag_BooleanInput
On entry: if validate is set to Nag_TRUE argument checking will be performed. If validate is set to Nag_FALSE 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 9 for further details.
2: – const IntegerInput
On entry: elements
m1 to m2
contain integer values to be searched.
m1 to m2
of iv must be sorted in ascending order.
3: – IntegerInput
On entry: the index of the first element of iv to be searched.
4: – IntegerInput
On entry: the index of the last element of iv to be searched.
5: – IntegerInput
On entry: the sought-after item.
6: – NagError *Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).
6Error Indicators and Warnings
Note: these errors will only be returned if .).
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument had an illegal value.
On entry, .
On entry, , .
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.
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.
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 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 m01nbc is , where , when .
This example reads a list of integer numbers and sought-after items and performs the search for these items.