Recurrence relation: The time to search in an array of N elements is equal to the time to search in an array of N/2 elements plus 1 comparison.
T(N) = T(N/2) + 1
Next we perform telescoping: re-writing the recurrence relation for N/2, N/4, …, 2
T(N) = T(N/2) + 1
T(N/2) = T(N/4) + 1
T(N/4) = T(N/8) + 1
……
T(4) = T(2) + 1
T(2) = T(1) + 1
Next we sum up the left and the right sides of the equations above:
T(N) + T(N/2) + T(N/4) + T(N/8) + … +T(2) =
T(N/2) + T(N/4) + T(N/8) + … +T(2) + T(1) + (1 + 1 + … + 1)
The number of 1’s on the right side is LogN
Finally, we cross the equal terms on the opposite sides and simplify the remaining sum on the right side:
T(N) = T(1) + LogN
T(N) = 1 + LogN
Therefore the running time of binary search is:
T(N) = O(LogN)