Analysis of Binary Search
The following is the code for a binary search. Similar to linear search, we make an assumption that the size() function has a constant run time. For a binary search to work the data must be sorted. In this case we assume that the data is sorted from smallest (at arr[0]) to biggest (at arr[size-1]).
The second part that is important to remember about a binary search is that you need to be able to access any item given its index in constant time. If that is not possible, the analysis will fail.
Like a Linear search, the determining factor on runtime for binary search is also the loop:
The code within this loop is constant, meaning that no matter what size is one iteration of the loop will take the same number of operations (more or less) and thus, the question really becomes how many times will this loop run?
Again, we can either find the key or not find the key.
If the vector does NOT contain the key, how long will it take to run?
At the beginning high - low = n -1. With each iteration we "move" high or low towards each other so that their difference is halved (their new values are near the mid point)
By doing this, it would take the loop at most log n iterations before low>high
For a successful search we might still need to search when high==low and to get to that point would also require iterations. Thus, the worst case runtime for this function when the key is found is also O(log n)
Last updated