Binary Search In C Program Using Recursion
5 stars based on
Searching and Sorting This chapter describes functions for searching and sorting arrays of arbitrary objects. You pass the appropriate comparison function to be applied as an argument, along with the size of the objects in the array and the total number of elements. In order to use the sorted array library functions, you have to describe how to compare the elements of the array. To do this, you supply a comparison function to compare two elements of the array.
The library will call this function, passing as arguments pointers to two array elements to be compared. Here is an example of a comparison function which works with an array of numbers of type double:.
This type is a GNU extension. To search a sorted array for an element matching the key, use the bsearch function. The array binary search program in c using function and pointers count elements, each of which is of size size bytes. The compare function is used to perform the comparison.
This function is called with two pointer arguments and should return an integer less than, equal to, or greater than zero corresponding to whether its first argument is considered less than, equal to, or greater than its second argument. The elements of the array must already be sorted in ascending order according to this comparison function. The return value is a pointer to the matching array element, or a null pointer if no match is found. If the array contains more than one element that matches, the one that is returned is unspecified.
This function derives its name from the fact that it is implemented using the binary search algorithm. To sort an array using an arbitrary comparison function, use the qsort function. The array contains count elements, each of which is of size size.
The binary search program in c using function and pointers function is used to perform the comparison on the array elements. If two objects compare as equal, their order after sorting is unpredictable. That is to say, the sorting is not stable. This can make a difference when the comparison considers only part of the elements. Two elements with the same sort key may differ in other respects. If you want the effect of a stable sort, you can get this result by writing the comparison function so that, lacking other reason distinguish between two elements, it compares them by their addresses.
Note that doing this may make the sorting algorithm less efficient, so do it only if necessary. Here is a simple example of sorting an array of doubles in numerical order, using the comparison function defined above see section Defining the Comparison Function:. The qsort function derives its name from the fact that it was originally implemented using the "quick sort" algorithm.
Here is an example showing the use of qsort and bsearch with an array of structures. Binary search program in c using function and pointers objects in the array are sorted by comparing their name fields with the strcmp function. Then, we can look up individual objects based on their names.