function
<cstdlib>
bsearchvoid* bsearch (const void* key, const void* base, size_t num, size_t size, int (*compar)(const void*,const void*));
Binary search in array
Searches the given key in the array pointed to by base (which is formed by num elements, each of size bytes), and returns avoid*
pointer to a matching element, if found.
To perform the search, the function performs a series of calls to compar with key as first argument and elements of the array pointed to by base as second argument.
Because this function may be optimized to use a non-linear search algorithm (presumably a binary search), the elements that compare less than key using compar should precede those that compare equal, and these should precede those that compare greater. This requirement is fulfilled by any array ordered with the same criteria used by compar (as if sorted with qsort).
void*
.
void*
.
1
int compar (const void* pkey, const void* pelem);
Taking two pointers as arguments: the first is always key, and the second points to an element of the array (both type-casted to const void*
). The function shall return (in a stable and transitive manner):
<0
The element pointed to by pkey goes before the element pointed to by pelem 0
The element pointed to by pkey is equivalent to the element pointed to by pelem >0
The element pointed to by pkey goes after the element pointed to by pelem
1
2
3
4
5
6
int compareMyType (const void * a, const void * b)
{
if ( *(MyType*)a < *(MyType*)b ) return -1;
if ( *(MyType*)a == *(MyType*)b ) return 0;
if ( *(MyType*)a > *(MyType*)b ) return 1;
}
0
), this may point to any of them (not necessarily the first one).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* bsearch example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort, bsearch, NULL */
int compareints (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int values[] = { 50, 20, 60, 40, 10, 30 };
int main ()
{
int * pItem;
int key = 40;
qsort (values, 6, sizeof (int), compareints);
pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
if (pItem!=NULL)
printf ("%d is in the array.\n",*pItem);
else
printf ("%d is not in the array.\n",key);
return 0;
}
int
values and returns the result of subtracting their pointed values, which gives 0
as result if they are equal, a positive result if the value pointed to by a is greater than the one pointed to by b or a negative result if the value pointed to by b is greater.
In the main function the target array is sorted with qsort before calling bsearch to search for a value.
Output:
For C strings, strcmp can directly be used as the compar argument for bsearch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* bsearch example with strings */
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort, bsearch, NULL */
#include <string.h> /* strcmp */
char strvalues[][20] = {"some","example","strings","here"};
int main ()
{
char * pItem;
char key[20] = "example";
/* sort elements in array: */
qsort (strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);
/* search for the key: */
pItem = (char*) bsearch (key, strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);
if (pItem!=NULL)
printf ("%s is in the array.\n",pItem);
else
printf ("%s is not in the array.\n",key);
return 0;
}
log2(num)+2
times.
If key does not point to an object size bytes long, or if base does not point to an array of at least num properly arranged elements of size bytes each, or if comp does not behave as described above, it causes undefined behavior.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4