C++ – how to prove the complexity of an algorithm mathematically

algorithmscmath

I know the fundamental algorithms and it's complexities. for e.g if Binary Search has Complexity O(log n), then how I can mathematically prove this?

Best Answer

I assume you're asking about how to measure algorithm time complexity with regards to its input (how the algorithm time grows as N grows). One way is to model the algorithm in the form of a recurrence equation and then solve via a number of techniques. Common techniques are master theorem, substitution, recurrence trees, ...

The binary search algorithm can be seen as recurrences of dividing N in half with a comparison. So T(n) = T(n/2) + 1. Solve this by the master theorem to show the function is log n.

For a complete overview of this type of stuff I suggest working through these two classes:

http://aduni.org/courses/discrete and http://aduni.org/courses/algorithms