Time complexity of binary search


Best case


This will arise when our key matches with the key at mid.So we will need only 1 comparison.

Time complexity = O(1)


Worst case


This case will arise when our key is not present in the array.In this case, the number of comparisons would be the times n can be divided by 2 to get 1.

Let n = no of array elements , c = no of comparisons required





Time Complexity = O(log n)


Lets take an example to verify the comparisons required.Here n = 8






No comments:

Post a Comment