The concept of divide and conquer that you learnt about in the previous video leads to another search algorithm called Binary Search. If you understood the phonebook example well, you already know what binary search is. Let’s now explore it further.
So, for binary search, you need a sorted array. You can not apply binary search on an array thats not sorted. You start at the middle index and compare the element you’re searching for with the element at the middle index. If the element at this index does not match the element you’re searching for, you check if it is greater or lesser than the element at the index. If it is greater than the element at the middle index, you discard everything to the left and move to the right. You then find the middle of this array that’s to the right, compare the required element with its middle, and keep doing this till you find the required element. In the next video, you will get an intuition to why Binary Search takes significantly less number of steps than Linear Search.
Now suppose you are given a sorted array like this. So in a sorted array, all the elements are arranged in an increasing order. Now, suppose I say that I have to find an element x here and say x is equal to 23. So the technique which you learned till now was linear search. So what near search used to do? It used to compare each and every element of the array with the element which I want to find. But can we utilize the fact that this array A is sorted in the increasing order? Can we perhaps decrease the number of steps which I would require to find my element x equals to 23? Think about it and write your answers in the question that follow. So the algorithm which utilizes the fact that I have a sorted array here is known as the binary search. Now, as you know that if I would have done a linear search on this, it would have taken me 123456 number of steps to arrive at my element 23. Now we'll see how I can reduce these six number of steps using binary search. Let us move step by step. So in the first step, what I'll do is that I'll find the middle element of this array. Now, since this array consists of ten elements, so my fifth element is going to be my middle one, which is 16 in this case. Now, if you think about it carefully, you would know that since all the elements are arranged in an increasing order, so the elements which lie towards the left of 16 will definitely be less than 23 because 16 itself is less than 23. So in my next step, what I can do is that I can safely ignore this left portion of my array. In the next step, my array would then be reduced to all the elements which lie towards the right of 16. Let me write my new array here then.
So this denotes the array in my second step. Now, if I have to find 23 here, I'll again repeat the same procedure and find the middle element of this array. Since this consists of five elements, my middle element would be 56. On comparing 56 with 23, I find that since 56 is greater than 23, so if 23 were existing inside this array, it would definitely lie towards the left of 56. So what I'll do, I will ignore the right hand portion of this array and consider only the left portion. Now, in my next step I would see that my array has been reduced to this.
Here again, I would find the middle element of this array which will be this element. On comparing it with 23 I would find that since x a which was 23 equals this element. So I have successfully found out my element number 23 here. And how many steps did it take? It took me exactly three number of steps to arrive that 23 exists here in this array, at this index. Compare it with the six number of steps you took to find in linear search. Here in binary search, we took half number of steps, which was three. So binary search is definitely a very good way of finding a certain element given in a sorted array.
How to find an element in a sorted array using binary search algorithm
Linear search involves comparing each element with the given element and is time-consuming
Binary search utilizes the fact that the array is sorted in increasing order to reduce the number of steps required to find the element
Binary search involves finding the middle element of the array and comparing it with the given element
Depending on the comparison result, either the left or the right portion of the array is ignored in the next step
This process is repeated until the element is found
Binary search algorithm takes fewer steps compared to linear search, making it an efficient way to find an element in a sorted array.
Let's now move onto the next segment where we discuss the pseudocode for Binary Search algorithm.