 Hello everyone. I'm Mrs. Meenakshi Sriyandi, assistant professor from Walshchand Institute of Technology, Shalapur. Welcome to the video lecture on binary search. Learning outcome at the end of this session, students will be able to implement binary search using C programming. Let us see what is searching is. Searching is the process of finding a given data in a list of data. It decides whether a search key is present in the data or not. It is the algorithmic process of finding a particular item in a collection of items. Let us see what are the searching methods are. Sequential search or linear search. Sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. Binary search. Binary search is also known as half interval search. Logatimic search or binary job is a search algorithm that finds the position of a target value within a sorted array by repeatedly dividing the search interval in half. Hashing. Hashing is the process of converting a given key into address. A hash function is used to generate the address according to a mathematical algorithm. The result of a hash function is known as a hash value or simply a hash. In this video we are going to discuss on binary search. So let us see what is binary searches. Binary search is also known as half interval or logarithmic search is a search algorithm that finds the position of a target value within a sorted array. It only works on a sorted array. It starts the search by comparing the target element with the middle element of the array. If the target element is less than the middle element then the search is carried out in the first half of the array and if the target element is greater than the middle element then the search is carried out in the second half of the array. Let us see the illustration of binary search. Consider an array a which consists of 15 elements and and where all the elements are sorted and our task is to find the target element 16 from this array and we know that the array always begins with an index zero and it ends at the index with size minus one. Now to find the target element we need two pointers. One of the pointer that is low is initialized to zero 0th index and another pointer high is initialized to size minus one index that is high is initialized to 14th index and every time we need to take care that the value of low is always less than high. Based on the pointers of low and high we need to compute the third pointer called as mid and it is given in this form that is low plus high divided by two. Low is zero and high is 14 that divided by two gives 17. So the position of mid pointer is placed at 17. Now we need to compare a of seventh value with the target element. Here we can see that the target value is lesser than a of mid. For that we need to continue our search by adjusting high to mid minus one. So in the next iteration we need to adjust high to mid minus one. Now with the new position of low and high we need to compute the mid again. Low at present is zero and high is at six. Again we can see that low is lesser than high. Now after that we have computed mid which which gives the value three. So here mid is three. Now again a of mid is now compared with the target element. Here we can see that the comparison is not equal. Here the target value is greater than a of mid. For that we need to now adjust the low. For this we need to iterate again by adjusting the low to mid plus one. So in the next iteration low is adjusted to mid plus one. So with the new values of low and high we need to compute the mid value. Now again we can see that low value is at four and high is at six. So low is lesser than high. So based on this we have computed the mid value that is five. So the a of mid is now compared with the target element. Here we can see that the target element is lesser than a of mid. Now when it is lesser than a of mid we need to adjust high. That is high should be adjusted to mid minus one. So in the next iteration we need to adjust high to mid minus one. Now based on low and high value the new mid is computed. But before that we need to check that low is less than or equal to high. Yes low is less than or equal to high. So based on this mid is equal to four. So the new mid is placed at four. Now we need to compare a of mid with the target element. And we can see that the target element is equal to a of mid. So here we can see that the target is found at the index four of the array. Now let us see the illustration when the best case of binary search is achieved. For that consider an array where it consists of six elements and here we want to find the target element as 22. So here again low and high are placed. Low is initialized to 0th index and high is indexed at the sixth index. So based on this the mid value is computed that gives the value three. So mid is placed at the third index. Now here the target element is compared with the a of mid element. And here we can see that the target is equal to a of mid. And we have seen that the target is found at a of three. Now the best case of the binary search is achieved when in only one comparison the target element is found. Let us see the illustration when the worst case of the binary search is achieved. So again consider the same array and we hear and here we want to find the target element as 35. So here again low is adjusted to 0 and high is adjusted to the size minus 1th index. And based on this the mid value is computed that is 3. Now mid is at the index 3. So here the a of mid value and the target elements are compared. Here we can see that the target is greater than a of mid. So for that we need to address the low. So low is adjusted to mid plus 1. So in the next iteration low is adjusted to mid plus 1 and based on the new values of low and high the new mid is computed. So it gives the value 5. So here in this situation we can see that low is lesser than high. Then we have computed mid that gives 5. So mid is placed at the index 5. Now here we are comparing the target element with the a of mid element. And here we can see that target is lesser than a of mid. For that we need to go for the next iteration where high is adjusted to mid minus 1. So in the next iteration high is adjusted to mid minus 1 and based on the new values of high and low we need to compute the mid. But before computing mid we need to check whether low is lesser than or equal to high. So here we can see that low and high are placed at the same index. So based on this we have computing the mid value. It gives 4. So a of mid value is now compared with the target element. And in the comparison we can see that the target element is greater than a of mid. When it is greater then we need to address the high in high to mid minus 1. So in the next iteration when we are addressing high to mid minus 1 here we can see that. So here we can see that low is greater than high. And when this happens we can say that the target element is not there in the given array. So the worst case of the binary search is achieved when the target element is not placed in the array and all the comparisons aren't done. Let us see the advantages and disadvantages of binary search. Advantages, binary search is an optimal searching algorithm using which we can search desired element very efficiently. For large list of data it works significantly better than linear search. Disadvantages, this algorithm requires the list to be sorted. Binary search is somewhat complicated than linear search. Try to think and answer what is the algorithm for binary search. Pause the video for some time and note down the answer in your book. Let us see the algorithm for binary search. At the very first step input an array of an element and the target to be searched. And then initialize low to zero and high equal to n minus 1. And repeat steps 4 and 5 while low is lesser than or equal to high. And in the next step compute mid that is equal to low plus high divided by 10. Based on the mid compare the target with a of mid. If the comparison is equal then print the target has found and break the loop. If the target is lesser than a of mid then adjust high equal to mid minus 1. This indicates that the target is greater than a of mid. In that case we need to adjust low and low is said to mid plus 1. Coming out of the loop if low becomes greater than high then that indicates that the target is not found. Let us see the implementation of binary search. So at the very first step we are declaring the array of size 50 and all other variables like target, low, high and mid. This all variables are declared under integer. Then in the next step we are asking the user to enter how many elements he wants to enter in the array. And that value we are collecting by using a scan of statement into n. By using a loop we are collecting the elements into the array. And in the next step we are asking the user to enter the element to be searched. And that we are collecting into the variable called as target. In the next step we are initializing low to zero and high is initialized to n minus 1. Then we are using a while and it is iterated every time till low is lesser than or equal to high. And in each iteration we are computing the mid value by using this formula that is low plus high divided by 10. And the target element is compared with the array's mid element. And if the comparison is equal then we are displaying the message as target is found at the mid plus 1 position. And if it is found then we need to use a break statement to come out of the while loop. Else if we are comparing the target element whether it is lesser than or greater than the mid element. If the target is lesser than the mid element then we need to adjust the high to mid minus 1. Else indicates that the target is greater than the mid element. In that case we need to adjust low equal to mid plus 1. Out of the while loop we need to check whether low becomes greater than high. If low becomes greater than high it indicates that the target element is not present in the array. And here are the program terminates. So this is the implementation of binary search using C programming. These are my references. Thank you.