O(log N) == O(1) - Why not?

后端 未结 23 2004
广开言路
广开言路 2020-12-22 18:17

Whenever I consider algorithms/data structures I tend to replace the log(N) parts by constants. Oh, I know log(N) diverges - but does it matter in real world applications?

23条回答
  •  一生所求
    2020-12-22 18:58

    For any algorithm that can take inputs of different sizes N, the number of operations it takes is upper-bounded by some function f(N).

    All big-O tells you is the shape of that function.

    • O(1) means there is some number A such that f(N) < A for large N.

    • O(N) means there is some A such that f(N) < AN for large N.

    • O(N^2) means there is some A such that f(N) < AN^2 for large N.

    • O(log(N)) means there is some A such that f(N) < AlogN for large N.

    Big-O says nothing about how big A is (i.e. how fast the algorithm is), or where these functions cross each other. It only says that when you are comparing two algorithms, if their big-Os differ, then there is a value of N (which may be small or it may be very large) where one algorithm will start to outperform the other.

提交回复
热议问题