How to Use bsearch with an Array in C? Last Updated : 15 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The bsearch() is a library function in C that is used to perform binary search on a given sorted array. In this article, we will learn how to use search on an array in C. Example Input: arr[]= { 2, 5, 7, 11, 15 };Key=15Output:Value 15 found!bsearch() with an Array in CTo use bsearch() on a given array we need to pass the sorted array, key (the element we want to search), size of each element in the array, the total number of elements in an array, and a comparator function which will help the bsearch to find the matching value. The comparator function should return the result as If the first argument is less than the second, the result is a negative integer.If the first argument is greater than the second, the result is a positive integer.If the arguments are equal, the result is zero.the bsearch will return a pointer to the element if it is found otherwise it will return NULL. Syntax of bsearch() bsearch(key, ptr, num, size, comp);C++ Program to Use bsearch with an ArrayThe below example demonstrates the use of bsearch() with an array. C // Program to search for an element in an array using binary // search #include <stdio.h> #include <stdlib.h> // Comparator function for integer values int compare_ints(const void* a, const void* b) { return *(int*)a - *(int*)b; } int main() { // initializing an array int array[] = { 2, 5, 7, 11, 15 }; int num_elements = sizeof(array) / sizeof(array[0]); // key to be searched int key = 15; // Perform binary search void* found_element = bsearch(&key, array, num_elements, sizeof(int), compare_ints); // Check if the element is found if (found_element != NULL) { printf("Value %d found!\n", *(int*)found_element); } else { printf("Value not found.\n"); } return 0; } OutputValue 15 found! Time Complexity: O(log N) Auxiliary Space: O(1) Note: The array must be sorted before using bsearch because the behavior of bsearch is undefined in case the array is not sorted. Comment More infoAdvertise with us Next Article How to Use bsearch with an Array of Struct in C? R rajpootveerendrasingh36 Follow Improve Article Tags : C Programs C Language Binary Search c-array C Examples +1 More Practice Tags : Binary Search Similar Reads How to Use bsearch with an Array of Struct in C? The bsearch function in C is a standard library function defined in the stdlib.h header file that is used to perform binary search on array-like structure. In this article, we will learn to use bsearch with an array of struct in C. Input: struct Person people[] = { { 1, "Ram" }, { 2, "Rohan" }, { 4, 3 min read How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C.Initializing a Dynamic Arrays 2 min read How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read How to Pass Array of Structure to a Function in C? An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin 2 min read How to Search in Array of Struct in C? In C, a struct (short for structure) is a user-defined data type that allows us to combine data items of different kinds. An array of structs is an array in which each element is of struct type. In this article, we will learn how to search for a specific element in an array of structs. Example: Inpu 2 min read Return an Array in C In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C 5 min read Like