Open In App

bsearch() Function in C

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C programming language, bsearch() function is used to perform a binary search on a sorted array to search for an element. The name bsearch stands for “binary search” which is an efficient algorithm for finding an item from a sorted list of items. It is defined inside the stdlib.h header file.

Syntax of bsearch() in C

void* bsearch(key, base, num, size, compare);

Parameters of bsearch()

The bsearch() function takes five parameters:

  • key: Pointer to the element to search for.
  • base: Pointer to the first element of the array.
  • num: Number of elements in the array.
  • size: Size of each element in an array.
  • compare: Function pointer to a comparison function that compares the matching element.

Return Value of bsearch()

The bsearch() function returns two values:

  • The pointer to the matching element in the array if the key is found.
  • If the key is not found, it returns a NULL pointer.

bsearch() Comparator Function

The comparator function is passed as a parameter to the bsearch() function and it contains the logic to find the matching element. 

Signature of Comparator Function

int comparatorName(const void* ptr1, const void* ptr2);

Here, ptr1 and ptr2 are the pointers to the elements to be compared. All the comparator function should have the same signature.

The comparator function should return the result as:

  • If the first argument is less than the second, return a negative integer.
  • If the first argument is greater than the second, return a positive integer.
  • If the arguments are equal, return zero.

Example of bsearch() in C

The below example demonstrates how we can search an element using bsearch() in C.

C++
// C Program to search an element using bsearch() function
// in C
#include <stdio.h>
#include <stdlib.h>

// Comparison function for bsearch()
int compare(const void* a, const void* b)
{
    return (*(int*)a - *(int*)b);
}

int main()
{
    // Initialize array
    int arr[] = { 1, 2, 3, 4, 5 };

    // Calculate the number of elements in the array
    int n = sizeof(arr) / sizeof(arr[0]);

    // Define the key to search for
    int key = 3;

    // Declare a pointer to hold the result of the search
    int* item;

    // Sort the array using qsort()
    qsort(arr, n, sizeof(int), compare);

    // Search for the key in the array using bsearch()
    item
        = (int*)bsearch(&key, arr, n, sizeof(int), compare);

    // If the key is found, print its value and index
    // If not found, print a message indicating it was not
    // found
    if (item != NULL) {
        printf("%d Found at index %ld\n", *item,
               item - arr);
    }
    else {
        printf("Key = %d is not found\n", key);
    }
    return 0;
}

Output
3 Found at index 2

Time Complexity: O(log N), where n is the number of elements in the array.
Auxilliary Space: O(1)




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads