I have a sorted array, and a number, how many maximum comparisons should I do to find if the number is contained in the array ?
Suppose we have a million of numbers in the array.
I have a sorted array, and a number, how many maximum comparisons should I do to find if the number is contained in the array ?
Suppose we have a million of numbers in the array.
To complete the answer from @aponeme, the maximum number of comparisons is equal to
2*upper(log2(n))
The reason is that the size of the array you examine is equal to
n, n/2, n/4, ...n/(2^steps).
Then the maximum number of steps is such that
n/(2^nsteps) = 1, i.e. nsteps = log2(n)
With divide and Conquer or Binary search (pseudo - code):
Worst case scenario: Need to divide into two arrays each of length 1 => O(logN)