 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to write and explain a program that will be written in C language. And that is based on binary search on 1DM. And that is related to data structure and algorithm. So in this video series, I am explaining you all the data structure and algorithms, how we can implement that with the help of C language. So in this video, you will learn how to convert binary search in C language. So as you go through my video that explains you how we can write binary search algorithm. So there, I told you, whenever you want to apply binary search, so your array should be in sorted order. So let's say, for example, I am taking an array. So these are values. So 20 elements are available here and you can see all elements are arranged in a particular order that is sorted order. So when you index is 0.2345679. So total elements are 10. So index is starting from 0 and ending to 9. So in programming, first location index is 0. But in normal algorithm, we consider first location index as 1. So do remember these things. So based on this array, now I'm going to write a method or function in C language that will perform binary search on 1DM. So return type is white, function name is binary search. It will receive first parameter as array. Second parameter as size and third parameter as item, then I'm going to search. Right. So these three parameters will be required as input for this method. Then I'm declaring one variable as beginning. It is initialized with 0. Then another variable as end. It will be initialized with size minus 1. Because size means total number of elements. So total number of elements are 10. So last index will be 9 for the exercise size minus 1. And one more variable that is LOC and I'm initializing it with minus 1. So this LOC will be identifying on which location, on which index that number is available. Right. And apart from that, one more variable will be required. So I'm putting mama here and name of the variables will be met. So these variables will be required so that we can apply on calculation. So now after declaring these variables, I need to apply a loop. So my beginning is less than equals to n. So its value of beginning is less than equals to n. Now we need to calculate the mid position. So this I also explained an algorithm in the video that is based on algorithm. So we need to calculate the mid position with the help of beginning plus x by 2. So after completion of this program, I will take this array as an example and we will do a step by step understanding. So I will be explaining each and every step so that you can understand how it is working. So now I need to apply this condition. So here I'm writing if A of mid is double equals to item. So if array mid position value equals to item, it means number is there. So I can write LOC equals to mid. So number is available on the mid position. So that's why when you assign it to LOC and I can break the loop. Right. So this loop will be terminated to this if number is found. So unnecessarily don't want to repeat our loop. So it will be terminated. Now after this, you can apply as if this condition is false. Let's say number is not available on the mid position. So if number is not available on mid position, so it will be on the left-hand side of the array or right-hand side of the array. So let's say the item that we want to search is A D and if we identify the mid position so 0 and end is 9. So beginning is 0 and end is 9. So 9 plus 0 is 9 divided by 2. So mid will be 4. So it will be 4.5. So we are taking these up. So it will be 4. So 4 index value is 15. So A of 4 index value is 15. If it is equals to item, so right now it is not equal 15 and 18. So this condition will be false. So now we need to search this element either in this part of array or in this part of array. So now we need to check if A of mid. So now we are going to check A of mid is less than item. So if A of mid is less than item, so A of mid is 15. 15 is less than 18. Yeah, it is true. So it means number will be searched into this part. So you can see your end position is already on this. Now we need to change the beginning position. So beginning position will be mid person. So when this is true, so you can write beginning equals to mid plus 1. And if it is false, A of mid is not less than item. It is greater than item. A of mid is greater than item. So it means you need to move here. So beginning is already on you. So you need to change the end position. So end position with equals to mid minus 1. So as you can write and equals to mid minus 1. So this way this loop is completed here. So it is checking all the cases equal case, less than case or greater than this. And accordingly it is working. And after completion of this loop, you need to check if LOC not equals to minus 1. If location is not equals to minus 1, it means location is having any mid position index. So it means number is found. So you can write print as found at person D position and you can write LOC plus 1. So LOC will be having a particular index. And in real time, let's say number is 0 index. So 0 index is considered as first position. So that's why I am writing LOC plus 1 so that as user oriented positions will be displayed. And as if it is not equals to minus 1, then it will be printed. If it is equals to minus 1 in that case, number was not found. So you can print not found. And you can close your binary search function. And also what you can do, you can return this location from this function to the main function. And in main function, you can apply this if I or whatever logic you want. Right. So this way the complete function is implemented with an epoxy language. So you can say binary search algorithm is implemented with C here. So this part you already understood, like beginning is less than equals to it. So beginning is 0 and has 9. So it is true. We also identified mid at this point. We compare this, it is false in first case because A of mid is 15 and item is 18. So it is false. So we are coming here. If A of mid has an item, so it is true. So building will be first to mid plus 1. So mid is 4. So building value will be 5. So it means we need to search the element in this part of that. So building is 5 and is 9. So now again we need to check this condition. So beginning is less than equals to 1. It is true. Again calculate the mid position. So add 5 plus 9. So it is 14. 14 upon 7. So new mid position will be 7. So A of 7 equals to item. So A of 7 position is having 18, which is equals to item. So mid position index will be assigned into location and break with parameter. And after evaluation of loop, this gives condition we need to check. LOC is not equals to minus 1. It is true because LOC is having value as 7. So here it will print 7 plus 1, that is 8. So 18 is available on the 8th position of 1. So that output will be mid. So this way friends, with the help of this logic, I hope you understood how we can write a binary search algorithm with the help of C language. So implement this method in your compiler and call this method with the help of main function. So you need to read array in main function that pass that array into this binary search function so that it can check whether the number is available in that array or not. But also remember your array should be in sorted order. So if you are not reading array in sorted order, so first you need to sort the array, then you can call binary search algorithm so that current output will be this way. So this way I hope you understood how we can write binary search algorithm using C language. And if you want to write, if you want to understand how we can write its algorithm. So in the description of this video and at the end of this video, you will find links of this related to data structures and algorithm and another one is data structure with C. So in both the playlist, one is containing all the algorithm and another is containing algorithm implementation with D. So follow those videos so that you can have proper command on data structure subject. So I hope you understood whatever I explained in this video. Thank you for watching this video.