bsearch
bsearch
bsearch,是一個計算機函數。
bsearch
二分法搜索
void*bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *));
#include
#define NELEMS(arr) (sizeof(arr) / sizeof(arr【0】))
int numarray【】 = {123, 145, 512, 627, 800, 933};
int numeric (const int *p1, const int *p2)
{
return(*p1 - *p2);
}
int lookup(int key)
{
int *itemptr;
itemptr = bsearch (&key, numarray, NELEMS(numarray),
return (itemptr != NULL);
}
int main(void)
{
if (lookup(512))
printf("512 is in the table.\n");
else
printf("512 isn't in the table.\n");
return 0;
}