Complexity of Binary Search Algorithm - O(log n) - Data Structure

Q.  What is the Complexity of Binary Search Algorithm?
- Published on 25 Aug 15

a. O(n)
b. O(log n)
c. O(n2)
d. O(n log n)

ANSWER: O(log n)
 

    Discussion

  • Nihal   -Posted on 01 Apr 16
    Worst case complexity O (log n)
    Best case complexity O( 1 )
    Average case complexity O (log n)

    Binary search algorithm:

    In binary search, the array should be sorted. First, compare the item to be searched with the middle element of the array. If the item is found, then search end successfully. If not the array is divided into two part. First part contains all elements to the left of middle element and the other second part contains all the elements to the right side of the middle element.

    Due to sorted array, all the element in the first part will be smaller than the middle element and all the element in the second part will be greater than the middle element. If the item to be searched is less than the middle element, then this item will be in first half otherwise it will be in the second half. This process will be continue till we find the required item or get a subarray which does not have any element.

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)