Bifalgorithm post · Computer science one · Searching
How does bsearch() efficiently search a sorted array?
How does bsearch() efficiently search a sorted array? (Computer science one · Searching) — "In the standard library (stdlib.h) there is an additional binary se…
Ideas in this flowchart
- The standard library provides bsearch() for searching sorted arrays.
- bsearch() is an additional binary search function in stdlib.h.
- It searches a sorted array more efficiently.
- Prototype: void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
- Its parameters are the same as those of lfind().
- Search result behavior
- It returns a pointer to the first element it finds, which is not necessarily the first element in array order.
- It returns NULL when no matching element is found.
- The array must be sorted with the same comparator used for sorting; otherwise, NULL may be returned erroneously.