algorithmbinary-search

Number of comparison in best, avg and worst case in Binary Search


So I basically want to know what will be the Best, Avg and Worst case no of comparisons taken by Binary Search on a Sorted Array of N elements. Consider Both cases where the element is present and not present in the Array

I have went online but unable to get a reliable and satisfactory answer


Solution

  • I think the worst case likely occurs when the target element is absent from the array. Hope this answer will help you out.

    Please note that log2(n) is rounded down to the nearest integer before adding 1. For example, if n = 10, log2(n) is approximately 3.32, which rounds down to 3. Adding 1 gives 4 comparisons in the worst case.