 Welcome to the lecture on running time of binary search. At the end of this session, students will be able to analyze the time complexity of binary search algorithm, derive the time complexity of binary search. Binary search is an efficient algorithm for finding an element that is an item from sorted list of item. Binary search works on the principle of divide and conquer strategy. In the analysis of binary search, here the strategy is to repeatedly divide the array into two equal parts considering a mid-element, which has index high plus low, where low and high are the starting and the last element of the array as shown in the visual given here. Now, in order to search the value x, the working concept of binary search is as described on this slide. If x is the mid-element, it means that the number has been found and the index of the mid-element is written. If x is less than mid-element, then the search the first half of the array. If x is greater than mid-element, then search the second half of the array. Here it is important to know that when we search the first or the second half, we apply the same rule or that is the same three principles again. So it must be noted that binary search is a divide and conquer algorithm. This concept will be better understood by going through the following illustration. Consider a sorted array of 13 elements as given here. The sorted elements consists of the values filled in the box. Let us say we want to search 201. Now finding the given element. To find 201, there will be many iterations with each iterations having the steps as mentioned in the figure. Here low is 0 and high is 12. Select the middle element. The mid-element here is 101. Since 201 is greater than 101, we divide the array into two half and consider the sub-array after element 101. Now this sub-array with elements after 101 will be taken for the second iteration. For the second iteration, the first half is discarded and the elements after 101 will be taken into consideration. As we can see here, the new low is at 7 and high is at 12 index. Now we calculate the mid. The mid here is 179. Since 201 is greater than 179, we once again divide the array into two halves and consider the sub-array after element 179. Now the sub-array with elements after 179 will be taken into the next iteration that is iteration 3. Now the third space is further divided as seen in the visual. Now the array consists of only 201, 356 and 1000. Select the middle element. Now the mid-element is 356. Since 201 is greater than, that is sorry, as 201 is smaller than 356, we divide the array into two halves and consider the sub-array before 356 now. Now the sub-array with the elements before 356 will be taken into consideration for the next iteration that is the fourth iteration. Now the array consists of only one element that is 201 with same low and mid. And now the middle element is 201. Since 201 is the middle element, now the iterations will stop. The item is found. The number to be searched is found at the mid location. We can now see this entire search in the given visual. How the search space is divided from the first iteration to the second to the third and we find the element over here. If we see the search space gets divided. Now calculating the time complexity of binary search. For analyzing the time complexity of binary search, we make use of power 2 as the name suggests binary. As seen in the given diagram, binary search basically reduces the search space to half at each step. By search space, we mean the sub-array of the given array where the target value is located if present in the array. Initially, the search is the entire array and binary search redefines the search space at every space. The space in yellow represents the new space after every iteration. It does so by comparing the middle value in the search space to the target value. If the target value matches the middle element, its position in the array is returned else it discard half of the search space based on the comparison result. In binary search, we first compare the given element x with the middle of the array. If x matches with the middle element, then we return middle indexed. Otherwise, we either recur for left half of the array or the right half of the array as shown over here. Problem size n, it gets divided into the sub-problem size of n by b and till it reduces to 1. So, here at a iteration, the array is divided into half. So, let's say the length of array at any iteration is n. At first iteration, the length of the array is equal to n. At second iteration, the length of array got divided to n by 2. At third iteration, n by 2 raised to 2. Therefore, at iteration k, the length of the array is n by 2 raised to k. So, after k divisions, the length of array becomes 1. Therefore, length of array is equal to that is n by 2 raised to k is equal to 1. That is, n is equal to 2 raised to k. Applying the log function on both sides, log to the base 2n is equal to log to the base 2 to raise to k. We can see that ultimately, k is equal to log 2 to the base 2 to the n. Hence, the complexity. Here, as we see, at each iteration, the search space gets divided by 2 till k becomes 1. So, x is equal to the number of iterations. Therefore, binary search terminates after k iterations. In the above example, here k was equal to 4. Hence, the time complexity of binary search is o log n. Further, binary search complexity is further classified into 3. The worst case, the best case and average case. The worst case of binary search occurs when the element to be searched is not present in the array. And hence, one needs to traverse the complete steps in the algorithm to reach the conclusion. Hence, the time complexity of binary search at worst case is o log n with a space complexity of o of 1. As we will not declare another array to store the element to be found, we are simply returning the index of the key element to the main function and simply printing its value. That is, if the element is not present only, the worst case complexity of binary search becomes o log n. Whereas, the best case complexity becomes o of 1. That is, when the number to be searched happens to be the middle element of the array. Best case and average case complexity together are the same. Pause the video for some time and answer the following. Binary search is an efficient algorithm for finding an item from an unsorted list of item. True or false? The answer is B. Binary search is an efficient algorithm for finding item from a sorted list. Here it is mentioned as unsorted. Next, the time taken by binary search algorithm to search a key in a sorted array of n element is the answer is a o of log n. That is, in searching a sorted array by repeatedly dividing the search interval in half, therefore it takes maximum of log n searches to search an element from a sorted array. These are the references. Thank you.