BSEARCH(3C) — Silicon Graphics
NAME
bsearch − binary search
SYNOPSIS
char ∗bsearch ((char ∗) key, (char ∗) base, nel, width, compar)
unsigned nel, width;
int (∗compar)( );
DESCRIPTION
Bsearch is a binary search routine generalized from Knuth (6.2.1) Algorithm B. It returns a pointer into a table indicating where a datum may be found. The table must be previously sorted in increasing order according to a provided comparison function. Key points to the datum to be sought in the table. Base points to the element at the base of the table. Nel is the number of elements in the table. Width is the width of an element in bytes; sizeof (*key) should be used. Compar is the name of the comparison function, which is called with two arguments that point to the elements being compared. The function must return an integer less than, equal to, or greater than zero; accordingly, the first argument is to be considered less than, equal to, or greater than the second.
DIAGNOSTICS
A NULL pointer is returned if the key cannot be found in the table.
NOTES
The pointers to the key and the element at the base of the table should be of type pointer-to-element, and cast to type pointer-to-character.
The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared.
Although declared as type pointer-to-character, the value returned should be cast into type pointer-to-element.
SEE ALSO
lsearch(3C), hsearch(3C), qsort(3C), tsearch(3C).
Version 3.6 — December 20, 1987