xref: /third_party/musl/src/stdlib/bsearch.c (revision 570af302)
1#include <stdlib.h>
2
3void *bsearch(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *))
4{
5	void *try;
6	int sign;
7	while (nel > 0) {
8		try = (char *)base + width*(nel/2);
9		sign = cmp(key, try);
10		if (sign < 0) {
11			nel /= 2;
12		} else if (sign > 0) {
13			base = (char *)try + width;
14			nel -= nel/2+1;
15		} else {
16			return try;
17		}
18	}
19	return NULL;
20}
21