Search code examples
algorithmperformancecomplexity-theorynotation

Why can't we use O-Notation to compare algorithms?


From my textbook:

O-notation and Complexity of Algorithms

It is important not to try and make comparisons between algorithms using O-notation.

For example, suppose algorithm A1 and A2 both solve the same problem, A1 has complexity O(n^3) and A2 has complexity O(n^2).

The above statements are perfectly reasonable.

Observe that we cannot conclude that A2 is more efficient than A1 in this situation!

Why not? Complexity of A2 grows slower than A1.


Solution

  • Grows slower doesn't mean absolutely faster.

    Do you have the experience that your friend was taller than you when you were younger, but you end up be the taller guy between you two, or the other way around?

    That's the same meaning. A1 may be way more suitable and fast to solve a small scale problem. It just becomes slower when encountering big problems.

    If you want to know more detail about the mathematical background, then "An Introduction to the Analysis of Algorithms" by Robert Sedgewick is highly recommended.