Binary search for the worst case.

141 Views Asked by At

I want to analyze binary search for the worst case, completely mathematically without any ellipses(...).

I solved out the recurrence of the binary search. $$ T(n+1)=T(n/2)+C $$

I've already searched many materials and books. Many said $T(n)=T(n/2)+C$. And they solved some tree pictures or master method. And the result is $\log{n}$

Can someone solve $T(n+1)=T(n/2)+C$ without approximation or any tricks?

The code below is pseudo code.

BINARY_SEARCH(A, target):
left = 1
right = length(A)
while left < right
    mid = (left + right) / 2
    if A[mid] == target
        return mid
    if A[mid] < target
        left = mid + 1
    else
        right = mid - 1
return NIL

Thank you.