Search code examples
algorithmsearchtime-complexitybinary-searchasymptotic-complexity

Time complexity for modified binary search which calculates the mid as high - 2


I have to find out the time complexity for binary search which calculates the dividing point as mid = high - 2 (instead of mid = (low + high)/2) so as to know how much slower or faster the modified algorithm would be


Solution

  • The worst-case scenario is that the searched item is the very first one. In this case, since you always subtract 2 from n, you will have roughly n/2 steps, which is a linear complexity. The best case is that the searched item is exactly at n-2, which will take a constant complexity. The average complexity, assuming that n -> infinity will be linear as well.