Open In App

std::bsearch in C++

Last Updated : 31 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

std::bsearch searches for an element in a sorted array. It performs binary search on the sorted array to search for an element. The std::search function is defined inside the <cstdlib> header file.

Syntax

void* bsearch(const void* key, const void* ptr, size_t num, size_t size,
              int (*comp)(const void*, const void*));

Parameters

  • key: It is the element to be searched.
  • ptr: It is the pointer to the first element of the sorted array.
  • num: It is the number of elements in the array.
  • size: It is the size of each element of the given array in bytes.
  • comp: It is the comparison function that returns an integer based on the comparison. It returns:
    • a negative integer value if the first argument is less than the second.
    • a positive integer value if the first argument is greater than the second.
    • zero if the arguments are equal.

Return value

  • If the key is present, the pointer to the key is returned.
  • If the key is not present, a NULL pointer is returned.
  • It returns one of the positions of the key in case of duplicate elements.

Examples of bsearch()

Example 1:

The below C++ program performs the binary search in a sorted array.

C++




// CPP program to implement
// std::bsearch
#include <bits/stdc++.h>
using namespace std;
 
// Binary predicate
int compare(const void* ap, const void* bp)
{
    // Typecasting
    const int* a = (int*)ap;
    const int* b = (int*)bp;
 
    if (*a < *b)
        return -1;
    else if (*a > *b)
        return 1;
    else
        return 0;
}
 
// Driver code
int main()
{
    // Given array
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
 
    // Size of array
    int ARR_SIZE = sizeof(arr) / sizeof(arr[0]);
 
    // Element to be found
    int key1 = 4;
 
    // Calling std::bsearch
    // Typecasting the returned pointer to int
    int* p1 = (int*)bsearch(&key1, arr, ARR_SIZE,
                            sizeof(arr[0]), compare);
 
    // If non-zero value is returned, key is found
    if (p1)
        cout << key1 << " found at position " << (p1 - arr)
             << '\n';
    else
        cout << key1 << " not found\n";
 
    // Element to be found
    int key2 = 9;
 
    // Calling std::bsearch
    // Typecasting the returned pointer to int
    int* p2 = (int*)bsearch(&key2, arr, ARR_SIZE,
                            sizeof(arr[0]), compare);
 
    // If non-zero value is returned, key is found
    if (p2)
        cout << key2 << " found at position " << (p2 - arr)
             << '\n';
    else
        cout << key2 << " not found\n";
}


Output

4 found at position 3
9 not found

Example 2:

The below C++ program demonstrates the behavior of std::bsearch with duplicate elements.

C++




// C++ program to illustrate the bsearch()
#include <cstdlib>
#include <iostream>
using namespace std;
 
// comparator function
int compar(const void* a, const void* b)
{
    int key = *(const int*)a;
    int element = *(const int*)b;
 
    if (key < element)
        return -1;
    if (key > element)
        return 1;
    return 0;
}
 
// driver code
int main()
{
    int arr[] = { 2, 4, 6, 6, 8, 10, 10, 12, 14 };
    int key = 10;
 
    // using bsearch on arr for key
    int* result = (int*)bsearch(
        &key, arr, sizeof(arr) / sizeof(arr[0]),
        sizeof(arr[0]), compar);
 
    // cheking the value of the returned pointer
    if (result != nullptr) {
        cout << key << " found at index: " << (result - arr)
             << endl;
    }
    else {
        cout << key << " not found." << endl;
    }
 
    return 0;
}


Output

10 found at index: 6

Time Complexity: O(logN), where N is the number of elements.

Space Complexity: O(1)



Next Article
Article Tags :
Practice Tags :

Similar Reads