w3resource

C bsearch() function

C bsearch() function - Binary search a sorted table

Syntax bsearch() function

void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))

The bsearch() function is used to perform a binary search of an array of num elements, each of size bytes. The array must be sorted ascendingly by the function pointed to by compare.

Parameters bsearch() function

Name Description Required /Optional
key Pointer to the key to search for. Required
base Pointer to the base of the search data. Required
nitems Number of elements. Required
size Size in bytes of each element in the array. Required
compare Function that compares two elements. Required

Return value from bsearch()

  • This function returns a pointer to an entry in the array that matches the search key.
  • If key is not found, a NULL pointer is returned.
  • If two keys are equal, the element that key will point to is unspecified.

Example: bsearch() function

The following example shows the usage of bsearch() function.

#include <search.h>
#include <string.h>
#include <stdio.h>

int compare( char **arg1, char **arg2 )
{
   /* Compare all of both strings: */
   return _strcmpi( *arg1, *arg2 );
}

int main( void )
{
   char *colors[] = {"red", "green", "pink", "orange", "red", "white", "black", "blue"};
   char **result;
   char *key = "red";
   char *key1 = "yellow";
   char *key2 = "black";
   int i;

   /* Sort using Quicksort algorithm: */
   qsort( (void *)colors, sizeof(colors)/sizeof(colors[0]), sizeof( char * ), (int (*)(const
   void*, const void*))compare );
   printf("Sorted array elements:\n");
   for( i = 0; i < sizeof(colors)/sizeof(colors[0]); ++i )    /* Output sorted list */
      printf( "%s ", colors[i] );

   printf("\n\nFind the color 'red' using a binary search algorithm:");
   result = (char **)bsearch( (char *) &key, (char *)colors, sizeof(colors)/sizeof(colors[0]),
                              sizeof( char * ), (int (*)(const void*, const void*))compare );
   if( result )
      printf( "\n%s color found at %Fp\n", *result, result );
   else
      printf( "\n'Red' color not found!\n" );
   
   printf("\n\nFind the color 'yellow' using a binary search algorithm:");
   result = (char **)bsearch( (char *) &key1, (char *)colors, sizeof(colors)/sizeof(colors[0]),
                              sizeof( char * ), (int (*)(const void*, const void*))compare );
   if( result )
      printf( "\n%s color found at %Fp\n", *result, result );
   else
      printf( "\n'yellow' color not found!\n" );
   
   printf("\n\nFind the color 'black' using a binary search algorithm:");   
   result = (char **)bsearch( (char *) &key2, (char *)colors, sizeof(colors)/sizeof(colors[0]),
                              sizeof( char * ), (int (*)(const void*, const void*))compare );
   if( result )
      printf( "\n%s color found at %Fp\n", *result, result );
   else
      printf( "\n'black' color not found!\n" );
}

N.B.: This code is executed in windows 10

Output:

Sorted array elements:
black blue green orange pink red red white

Find the color 'red' using a binary search algorithm:
red color found at 000000000062FDD8


Find the color 'yellow' using a binary search algorithm:
'yellow' color not found!


Find the color 'black' using a binary search algorithm:
black color found at 000000000062FDB0

C Programming Code Editor:

Previous C Programming: C getenv()
Next C Programming: C qsort()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming/stdlib/c-bsearch.php