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
I think the worst case likely occurs when the target element is absent from the array. Hope this answer will help you out.
log2(n)
comparisons, where n is the size of the array. This is because with
each comparison, binary search cuts the array in half. However, this
is an approximation and the exact number can vary slightly.log2(n) + 1
comparisons. This is because it takes log2(n)
comparisons to narrow down the search to a single element, and then one additional
comparison to confirm that the element is not there (or is the target
value).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.