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.
// 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" ;
} |
4 found at position 3 9 not found
Example 2:
The below C++ program demonstrates the behavior of std::bsearch with duplicate elements.
// 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;
} |
10 found at index: 6
Time Complexity: O(logN), where N is the number of elements.
Space Complexity: O(1)
FAQs on std::bsearch in C++
Q1. Where to use std::bsearch?
Ans:
Binary search can be used on sorted data where a key is to be found. It can be used in cases like computing the frequency of a key in a sorted list.
Q2. Why Binary Search?
Ans:
Binary search is much more effective than linear search because it halves the search space at each step. This is not significant for our array of length 9. Here, linear search takes at most 9 steps and binary search takes at most 4 steps. But consider an array with 1000 elements, here linear search takes at most 1000 steps, while binary search takes at most 10 steps. For 1 billion elements, binary search will find our key in at most 30 steps.
Binary search has a time complexity of O(log n), where n is the number of elements in the sorted list whereas linear search algorithms have a time complexity of O(n), binary search can be significantly faster for the list where the number of elements is very large.