'\"macro stdmacro
.if n .pH g3c.bsearch @(#)bsearch	40.12 of 10/26/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} bsearch 3C "C Programming Language Utilities" "\&"
.if \nX=1 .ds x} bsearch 3C "C Programming Language Utilities"
.if \nX=2 .ds x} bsearch 3C "" "\&"
.if \nX=3 .ds x} bsearch "" "" "\&"
.TH \*(x}
.SH NAME
\f4bsearch\f1 \- binary search a sorted table
.SH SYNOPSIS
.PP
\f4#include <stdlib.h>
.PP
\f4void \(**bsearch (const void \(**key, const void \(**base, size_t nel,
.br
.nf
    size_t size, int (\(**compar)(const void \(**, const void \(**)\|);\f1
.fi
.SH DESCRIPTION
\f4bsearch\fP
is a binary search routine generalized from Knuth (6.2.1) Algorithm B.
It returns a pointer into a table (an array) indicating where
a datum may be found
or a null
pointer if the datum cannot be found.
The table must be previously sorted in increasing order
according to a comparison function pointed to by \f2compar\f1.
.I key\^
points to a datum instance to be sought in the table.
.I base\^
points to the element at the base of the table.
.I nel\^
is the number of elements in the table.
.I size
is the number of bytes in each element.
The function pointed to by
.I compar
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 0
as accordingly the first argument is to be considered
less than, equal to, or greater than the second.
.SH EXAMPLE
The example below searches a table containing pointers
to nodes consisting of a string and its length.
The table is ordered alphabetically on the string in the
node pointed to by each entry.
.PP
This program reads in strings and either finds the
corresponding node and prints out the string and its length,
or prints an error message.
.PP
.RS
.nf
.ft 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {			/\(** these are stored in the table \(**/
	char \(**string;
	int length;
};
static struct node table[] =	/\(** table to be searched \(**/
{
	{ "asparagus", 10 },
	{ "beans", 6 },
	{ "tomato", 7 },
	{ "watermelon", 11 },
};

main()
{
	struct node \(**node_ptr, node;
	/\(** routine to compare 2 nodes \(**/
	static int node_compare(const void \(**, const void \(**); 
	char str_space[20];   /\(** space to read string into \(**/

	node.string = str_space;
	while (scanf("%20s", node.string) != EOF) {
		node_ptr = bsearch( &node,
			   table, sizeof(table)/sizeof(struct node),
			   sizeof(struct node), node_compare);
		if (node_ptr != NULL) {
			(void) printf("string = %20s, length = %d\en",
				node_ptr\(mi>string, node_ptr\(mi>length);
		} else {
			(void)printf("not found: %20s\en", node.string);
		}
	}
	return(0);
}

/\(** routine to compare two nodes based on an  \(**/
/\(** alphabetical ordering of the string field \(**/
static int
node_compare(const void \(**node1, const void \(**node2)
{
	return (strcmp(
			((const struct node \(**)node1)\(mi>string,
			((const struct node \(**)node2)\(mi>string));
}
.fi
.ft 1
.RE
.SH SEE ALSO
\f4hsearch\fP(3C), \f4lsearch\fP(3C), \f4qsort\fP(3C), \f4tsearch\fP(3C).
.SH DIAGNOSTICS
A null pointer is returned if the key cannot be found in the table.
.SH NOTES
The pointers to the key and the
element at the base of the table
should be of type pointer-to-\f2element\f1.
.PP
The comparison function need not compare every byte,
so arbitrary data may be contained in the elements in addition to the values
being compared.
.PP
If the number of elements in the table
is less than the size reserved for the table,
.I nel
should be the lower number.
.\"	@(#)bsearch.3c	6.3 of 10/20/83
.Ee
