 Hello friends, I am Sanjay Gupta. I welcome you to Sanjay Gupta Tech School. So in this video, I am going to explain you how you can write binary search algorithm. And this algorithm will be applied on what the area. So first understand how binary search algorithm works. So let's say I have this what the area. Total 10 blocks are available here. So whenever you want to apply binary search on any array. So you need to remember that your area should be in sorted order. So the conclusion is you can apply binary search only on sorted areas. If your area is not sorted. So first you need to sort all the elements rather than assigning or assigning order. Then only you can apply binary search on that area. So we are assuming that our array is already sorted. And it has these 10 elements, right? Now let's say I need to search an element that is 80. So these are the positions of the elements. So in algorithm, I already told you in my previous video that first position is demoted as 1. And last position has demoted as n. So here this first position it takes as 1. Last position it takes as n. So in case of binary search, first you need to identify the mid position. So mid will be identified by adding lower count and upper count and divided by 2. So here 1 plus count equals to 11 plus 2. So it will be 5.5 and we consider it as 5. So this fifth position will be mid position. So now we need to compare the cycle that we want to search, whether it is equal to the mid position value. So if it is equal to that position then it means number is off. If it is not equal then the number that we are going to search will be less than this value or it will be greater than this value. So that's why we need sorted add. So 18 will be compared to 15. So 18 is greater than 15. It means next we are going to search the element into this part of n, not in this part. And if the number you want to search is less than 15, then we need to search the element in this part of array, not in right inside part of it. And further, if we take this one, so this will be your lower count, this will be your upper count. Again you need to calculate the mid. And if your element is of the mid position, then number is off. Otherwise check it on the left side of the array or right inside. So this way we divide 1d array into 2 parts based on the mid position and either on left part or right part we further search the element. So this is the basic idea of binary search algorithm. Now I am going to like that algorithm so that at this point how we can implement the steps of binary search. So name of algorithm is binary search. Now I am going to write the variables that will be required for this algorithm. So these are the variables that will be required. So here a is sorted array that we have used, lb is lower count that will be 1, ub is the upper count that will be there, that item that we are going to search, and loc will be having the location where that element is available. So now I am going to write step number 1. So in case of step number 1, we need to set beginning equals to lower count. So beginning will be equals to lower count and the will be equals to upper count. So these variables values are initialized into beginning and end variable. And in step number 2, I am going to calculate bit position value. So we will be calculating bit to beginning plus end divided by 2. So with this formula bit position index will be identified. Now after this, so here also I get the same identified lower count, the upper count and through adding lower count with upper count divided by 2, I identify this bit fully. So same thing I wrote here. Now repeat steps. So here I am going to apply a loop. So let me tell you these things. So I am going to repeat step number 4 and 5. So step number 4 and 5 will be repeated. Why is less than equals to end? Why? Beginning is less than equals to end. A of mid position value is not equals to end. So this is the item that we are going to put. If it is not equal to A of mid position value and beginning is less than equals to end. So if both the conditions are true, then the step number 4 and 5 will be repeated. Otherwise this loop will be permanent. Now I have to write step number 4. So here I am going to check if item is less than A of mid. If item is less than A of mid. So let's suppose mid position is having 15 and item is less than mid. So in that case we need to search the element in the side. So here lower count is already set. We need to change the upper count value. So here we need to write set end equals to mid minus 1. So mid is positioning on fifth position. So mid minus 1 will be 4. So that will be assigned into end. So beginning will be pointing to this position and end is positioning to this position. And if this condition is false, it means item is greater than A of mid. Item is greater than A of mid. So in that case, beginning should be there and end should be here. So end is already on this position. So we need to change the beginning position. So else set beginning equals to mid plus 1. Then after setting these values, you need to calculate mid position again because we are working on this side or we are working on this part of edit. So again we need to identify the mid position. So after this, you need to write set mid equals to beginning plus end divided by 2. So inside this loop, again mid position will be calculated and this will be checked here. So beginning is less than equal to end and A of mid is not equal to item. So again these steps will be calculated. So after completion of this algorithm, again I will take this example and step by step it will follow like how it will be working. Now here I can write end of loop. So after completion of this loop, I need to write step number 6. So in case of step number 6, I need to check mid position with item. So if A of mid equals to item, then set LOC equals to. So here we need to assign the random of mid to LOC. So whenever A of mid equals to item, it means item is available on this mid position. So LOC will be assigned as mid. Otherwise set LOC equals to null or zero and then step number 7 will be end of this algorithm. So this rate, this algorithm is implemented. So now we are going to take this example so that you can understand this properly. So lower bound upper bound will be assigned to beginning or end. So let's say beginning is 1 and end is 10. Then we need to calculate the mid position. So 11 by 2, so mid position will be fine. So this is our mid position. And the item that we are going to search is 18. So check this condition. So it is true. A of mid not equals to item. So A of mid is 15. That is not equals to item. That is 18. So both the conditions are true. Now check this condition. Item is less than the outfit. So A of mid is 15. Item is 18. So again it is false. So else will be executed. So building equals to mid plus 1. So mid is 5. So building will be 6. So you can see. We need to search the element in this part of it from 6 to 10. Then again we need to calculate the mid. So this time building plus end. So building is 6 and is 10. So total is 16. 16 by 2 is 8. So new mid position will be 8. Again check this condition. Building less than equals to item. So A of mid is 2. A of mid not equals to item. So A of mid is 8. So 8th position value is equals to item. So this is false now. So this loop will stop or terminate here. And control will be transferred to this 6th scan. So here we need to check if A of mid equals to item. So it is true. So mid position value will be assigned to LOC. So LOC will be having value as 8. It means on 8th position that number is available. And this algorithm will be terminated. So this way with this process you can check whether a particular number is available in 1D array or not. Remember one thing. Whenever you want to apply binary search, your array should be arranged in sorted form. If it is not arranged in sorted form then your results will be incorrect. Because binary search algorithm prerequisite is sorted array. So I hope with this way you understood how we can implement binary search algorithm. If you want to watch how we can implement binary search algorithm with C language. So you can go to description of this video and you will find link of a playlist that has data structure you will see. So go to that playlist and there you will find a video based on binary search algorithm implementation in C language. So watch that so that you can understand how we can write a program based on binary search algorithm. So I hope you understood whatever I explained in this video. So thank you for watching this video.