To start with be careful about using the word "faster" when talking about Big-O complexity as it's done in the question title. Big-O says nothing about how fast an algorithm is. Big-O only tells how execution time changes when some variable N changes. Example:
O(1) algorithm : Doubling N will not change execution time
O(N) algorithm : Doubling N will double execution time (1 + 1 = 2)
O(N^2) algorithm : Doubling N will quadruple execution time (2 * 2 = 4)
Also notice that for some N values a O(N^2) algorithm may be faster than an O(N) algorithm. Big-O doesn't tell anything about that. All we can say is that if we keep increasing N then sooner or later the O(N) algorithm will be faster than the O(N^2) algorithm. But Big-O doesn't tell what that value of N is. It can be for N=1, N=10, N=100, ... So be careful about "translating" Big-O complexity into "fast".
Big-O is calculated as the number of times you need to perform some basic operation (an O(1) operation) as function of the variable N. Further, Big-O (normally) looks at worst case scenarios.
For an ordinary array the only basic operation we can perform is to look-up the value at some index i in the range 1..N
In a sorted array the returned value can be used to tell us three things (see later paragraph for exceptions):
- Is the value less than the value we are searching for
- Is the value greater than the value we are searching for
- Is the value equal to the value we are searching for
Now remember that Big-O is about worst case scenarious. So number 3 won't happen unless we are looking in a range with only one array element. So forget about number 3 for now.
Because the array is sorted the relevant answers can be translated into
- The search value is in range 1 ..
i
- The search value is in range
i+1 .. N
Now the question is: How to select the best value of i for the first look up?
Since Big-O is a worst case calculation, we shall always use the largest of the two ranges for the next step. To make the largest range as small as possible, we need to make the two ranges the same size. To do that we need i to be equal N/2. This is simply the best we can do with the knowledge we have from the look-up.
By doing that we have
- The search value is in range 1 .. N/2
- The search value is in range N/2+1 .. N
So in the next step we need to look in a range with N/2 elements.
Now apply the same again (i.e. i = N/2/2) for the next search to get down to N/4 element. Do it again to get to N/8 elements and so...
Repeat that until there is 1 element in the range - then we have the solution (number 3 above).
So each look-up reduce the range into half of it's original size. And k look-up will reduce the range by 2^k. Our target is to get a range size of 1 so we need to solve:
N / 2^k = 1 <=> N = 2^K <=> K = log2(N)
So from the assumption that all we can know from a look-up is whether our search value is to the left or right of the look-up position and from the fact that Big-O is worst case calculated, we can see that the search in a sorted array can't be any better than log(N) complexity.
The above covers the general array but notice that for some arrays the assumption may not hold. Sometimes an algorithm may extract more information from the look-up'ed value at index i. For instance if we know something about the distribution of values in the array and our search value is far from the look-up value, an algorithm may benefit from doing something else in the next look-up than doing the next look-up at N/2 and thereby be more efficient.