Analysis of Binary Search
The analysis of a binary search is not the same as that of linear search because the loop of a binary search does not follow the pattern of going from the start of the array all the way to the end. It starts in the middle of an array and jump around. Not every element will be considered during the search process so this will be a bit different.
A binary search can only be done to lists if two criteria are true:
the list must be sorted
access to any element given its position must have a constant run time.
In the above code, there are two variables low and high. These represent the first and last index of the elements where key may be located. In other words, the only possible places key may exist is somewhere between arr[low] and arr[high] inclusive. In each iteration of the while loop we check the middle element between them. If key is smaller than the middle element, we move the high marker so that it is to one element before the middle one. If key is bigger, we move the low marker one element after the middle one. In other words we eliminate half of the remaining possibilities of key's location after each iteration of the while loop. We stop when we either find it or we have eliminated all possible locations for key.
Analysis of an unsuccessful search
Similar to a linear search we should analyze the run time for what happens in its worst case. In this case, it is also performing an unsuccessful search
Let n represent the size of the array
Let T(n) represent the number of operations needed to find key within the array
In a binary search the following operators are done exactly one time:
The following are done for each iteration within the loop:
Within the loop there is also an if/else if/else statement. there are 3 paths through this statement. The path that would would lead the the highest operation count would be to follow the else-if. The reason is that we would end up doing both checks. and there are more operations in the else-if than the else statement. Thus, we should use that pathway
Thus what we can say is that each time the loop runs, it will perform 10 operations at worst
Number of Loop Iterations
The next question then is to figure out how many times this loop will run. We are of course assuming that the search will fail. Thus, the only way for this loop to stop is for low<=high to become untrue.
When we started off we have n elements to search. After one iteration through the loop we have approximately half of the original number of elements. After one more iteration we eliminate another half of the remaining elements.
the last iteration would involve only 1 element because low == high == mid, after that we would end up with low > high
Lets look at the pattern of the relationship between number elements within the search set and the iteration number.
Loop iteration | Number of elements between low and high inclusive (approx) | Denominator |
1 |
|
|
2 |
|
|
3 |
| |
4 |
|
|
5 |
|
|
... | ... | ... |
?? |
|
|
Now look at the exponent 2 was raised to in the denominator and the relationship with its loop iteration. If we can find what we need to raise 2 to in order to get n we will be able to find the number of iterations the loop will run.
By definition:
iff
In other words, is the exponent we need to raise to in order to get . That value is . Applied to our situation, the exponent we must raise to in order to get would be .
Our loop runs one time more than the exponent... thus our loop will run time.
Putting these ideas together, we can summarize the operation counts as follows:
Adding it all up we find the following expression for
The dominating term is . Thus, is
Last updated