 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain the code implementation of binary search algorithm. So, some students requested me like they want to see like how we can implement the complete code of binary search including the infrastructure and all. So, if you want to watch its classroom lecture explanation, so you can find that video link in the playlist so that link is available in the description of this video. So, you can find that link and if you want to study the lecture version of this binary search algorithm so you can do that as well. So, now I am going to implement its code so that you can see how binary search works in C. So, I will be showing you the method of binary search and I will be calling that into main function. So, first I am going to write a function that is binary search. It will be receiving three parameters. So, first is array, second is size and third is item that we are going to search. Then I am declaring some variable beginning and will be equals to n minus 1 then LOC equals to minus 1. And what else we need mid because we need to calculate the mid position while implementing binary search then after this I am implementing the condition beginning less than equals to and inside this I need to calculate mid so mid equals to beginning plus n divided by 2. Now, I need to check if a of mid position is equals to item. So, here I can assign mid to LOC variable and I can break this right if this condition is false then I can implement another if so if item is not available on the mid position so item may be available at the left hand side of mid value or right hand side of mid value. So, if item is less than a of mid if item is less than a of mid it means item is available at the left hand side part of the array. So, in that case, beginning will be on the zero and end will be the range so it will be mid minus one. Otherwise, item will be available available at the right hand side of the mid value. So, in that case, we need to set the beginning with mid plus one and the end variable will be pointing to the last location and after completion of this loop I can return value of LOC that will be received in main function. So, the step by step explanation I already explained in my lecture video so the link you can get from the description of this video. So, that is a complete playlist link so you need to search like binary search using C so you can watch that video as well. So, in that video I explained this part only like how binary search works. Now, in this video we will implement main function as well so that you will know like how the complete program works. So, I'm declaring 1D array sizes 20 variable I and item LOC. Now, I'm going to print the message and the size of array. Over send D, M% and size of array will be stored into end variable. Now, I'm going to implement a for loop which will repeat end times and it will be receiving the values from user. So, remember that whenever you are applying binary search to your input will be in form of sorted array. So, here, so before applying this loop we can print a message as well. Enter array in sorted form then print f enter a number. So, one by one numbers will be entered and they will be stored into array on ix position. So, this way all the elements of array will be received. Now, after this we need to read the number that we are going to search. So, again print f, you can print the message enter the number you want to search scan f backslash n for send D. Sorry, remove backslash n because we don't use it in scan f and the value will be stored into item variable. Now, LOC equals to binary search a comma n comma item. So, I need to pass three parameters into this binary search function. So, by the search function is defined here. So, expect the parameter array size and items. So, I'm passing those. Now, LOC will be having a value. So, if LOC is not equals to minus one, it means number is found. So, we can print on that for sending location. And LOC plus one, because in programming indexes are starting from zero, but in real life we consider first position as one. So, LOC plus one. So, if index is four, so it will be printing five, like fifth location. And else, we can print number is not gone. So, this way the program is implemented completely. So, this is the full implementation. And in my lecture video, I just explained the binary search functionality. But there you will be understanding it with the help of the upper example. And here I cannot draw an array and I cannot make every step so that video will be better for you to understand by research. And this is better to understand how we can implement a complete program in C language using function. So now I'm going to execute this code so that you can understand how it will be executed. So, I just forgot to use semicolon. So, I'm putting that. So, I'm entering sizes five. So, here you can see it is showing the message enter array in sorted form. So, I'm entering 12, 14, 16, 18 and 20. Now, I'm going to search 18. So, you can see it is showing found at fourth location. And if you see the order, so 18 is available on fourth location. Again, if I execute this code, I enter five as value, same numbers I'm entering again, and I'm going to search 13. So, you can see the message number is not gone. So, this way, you can implement binary search implementation code with the help of C language. So, I demoed you how you can implement a full program that works on binary search algorithm. Binary search works on sorted array. So, if you want to know how we can write its algorithm, and you want to understand the logic behind binary search. So, you will find link of playlist in the description of this video. Open those playlists and find the binary search related video and watch that so that you can understand that as well. I hope you understood how we can implement the complete code related to binary search with the help of an integrated development environment. So, thank you for watching this video.