 Hello learners, welcome to this video lecture and in today's video lecture we shall see how to use bsearch function which is an inbuilt function to search for a key element in a given array of elements. So by the end of this session you will be able to use bsearch function to search for a key element in an array of elements. So let us see and study about this bsearch function which has been already defined in this header file stdlib.h. So this is a standard inbuilt function which is available and ready to use in standard library header file. It performs a binary search on a sorted array. So before we apply or use this function we should know that the input that we are giving to this function should be in the sorted order. So this function returns a pointer to the key element if successful. That means if the key element that we need to search is available in the array that we are going to ask this function to search for if that key element is available in the array it will return a pointer to that element. If the element is available or found. If that element is not found then this function returns a null pointer. So let us discuss about the syntax of this particular function. So firstly it takes a parameter that is a pointer to the key value. This value is the value that we are searching for. The second one and the third, the fourth and the fifth parameter are quite similar to the Q sort function that we have seen earlier. If you have not gone through the video I encourage you to also watch the video on how to use Q sort function. So the second parameter that we have is the array that we want to search in the key element. The third parameter is the size of the elements. We can find out the size of the element that is how many number of elements are in our array. Firstly we firstly take the size of the elements and then take the input of the elements and the fourth parameter that we see here is the width of the elements. We will be finding the width of the elements using the size of operator and passing and passing integer to it if we want to search integer values or if the element array that we are going to pass as the base here is of integer type and so on and so forth. Now let us discuss about this compare, the fifth parameter. The fifth parameter that we see here is a pointer to a subroutine where two values are compared. The two values are the first one is the key value and the second one is the data element in the array. Now as a prerequisite to this video I assume that you know how binary search works. Binary search divides the array and the sub array into two parts and the mid of the array is always compared with the search element. Now comparison means the difference is taken between the two values. Now if the key value is less than the data that gives us an intimation that the key value is somewhere in the left sub array that we need to search. So when a difference of key value and data value is taken and the return value is less than zero it simply means that key is less than data and the left sub array is further checked for the key value. If the key value and the data value are same that means that we have got a hit and that the element is present in the array and so we come to the conclusion that key is equal to data and the value of zero is returned by the compare sub routine. When we take a difference of key and data and the difference is a positive value it simply means that key is greater than data and the key element is in the right sub array. So we move forward to the right sub array for detection of or to search the key element. So you can simply define the compare routine separately as given here. You can just take the difference between the pointers pointing to the two different values that is key and data and then typecast it into int and return it as integer and then you can call this compare or pass this compare sub routine as a parameter to the B search function. Now let us see the working of B search function. Now this is a program that I have written just for us to understand the working of B search function. We need to define the compare sub routine before we call it in the B search function. So it takes two pointers A and B respectively where A is the key and B is the data and then we pass the difference between A and B which is typecasted to int and returned to be considered as a parameter in the B search function. Next we start with our main function and take an array of size 10 to take our values as elements in the array. This is the size of the array and the key element is where we will take the input from the user to store the key element that needs to be searched. From line number 16 to 18 we take inputs of the size of the array and the elements that we need to enter as input into the array. As you can see on line number 20 I take the input of the key element that needs to be searched. On line number 23 as you can see we need to call or we need to sort the input of the data that we have received. As we have discussed earlier that B search function works on sorted data. So we need to sort the data that we are taking as input. For this you can call the inbuilt function Q sort to sort the data into ascending order. So I call the Q sort function which takes the four inputs as values that is our input array, size of the array that is stored in n, size of the data type which is the third parameter as size of int and the fourth parameter is a subroutine compare ints which is similar for Q sort and B search as well. Nextly what we do is we call the B search function in line number 26 and pass the five parameters that are the key value that we need to search, the array that we are storing the values, the size of the array that is the input of the input array, the width of the data type, we can find it by using the size of operator and the compare ints subroutine. When we call this function it returns a void pointer. Hence we convert the pointer into an integer pointer by type casting and then return that value to P item which is an integer pointer. Now as we had seen in earlier slides, if the value is not null, it means that the key value is present and P item will give us the pointer to the key value that is present in the array. If it returns null value, it simply means that it is not in the array and we will print that the key value is not in the array. Let us see how this program works. I take the size of the array first as input, say for example five. Then I take five random values that are as input of the array. Then I take the value that I need to search, say for example 200. This is the output that the program gives. Now once I hit enter here, the Q sort function will be called. And all this input values that I have given as input, which are stored in the array, input array will be sorted. So 100, 200, 400, 500 and 600 will be the order. And when it is compared, it is found that 200 is present in the array. And the pointer to this particular value is returned, which gives us this line that 200 is in the array. In case that I enter a value which is not present, say for example 900, we will get a value, we will get the output as 900 is not in the array. So at this point in time, I want you to pause the question. And as we have studied about B-search, I want you to tell me which header file is included or should be included in the program to execute B-search function. So you might have guessed it right. For using B-search function, we need to include stdlib.h header file, which is the same file which also holds the Q sort function. So these are the references. You may refer to these references to get into much detail of B-search function. Thank you very much.