Name

bisearch

Synopsis

int bisearch(void *sorted, void *target, int size, int esize, 
   int (*compare)(const void *key1, const void *key2);

Return Value

Index of the target if found, or -1 otherwise.

Description

Uses binary search to locate target in sorted, a sorted array of elements. The number of elements in sorted is specified by size. The size of each element is specified by esize. The function pointer compare specifies a user-defined function to compare elements. This function should return 1 if key1 > key2, if key1 = key2, and -1 if key1 < key2.

Complexity

O (lg n), where n is the number of elements to be searched.

Get Mastering Algorithms with C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.