img
Question:
Published on: 3 December, 2024

Find the time complexity of Binary Search Algorithm. 

Answer:

Time complexity of binary search

In binary search each step of the algorithm divides the list of items being searched in half of the list.

So we can say that

T(n)= T(n/2) +1    where  n>1

       = T(n/4) +1 +1

       = T(n/22) + 2

       = T(n/23) +3

     ……………..

     ………………

    =T(n/2k) + k

  If n=2k then k= log2n

    = T(1) +  log2n

    So

  T(n)= O(log2n)

Best case condition occur when the element to be searched in first time. Then complexity is O(1).

Random questions