What is Big O of a loop?

前端 未结 6 833
离开以前
离开以前 2020-12-03 15:02

I was reading about Big O notation. It stated,

The big O of a loop is the number of iterations of the loop into number of statement

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 15:33

    If you check the definition of the O() notation you will see that (multiplier) constants doesn't matter.

    The work to be done within the loop is not 2. There are two statements, for each of them you have to do a couple of machine instructions, maybe it's 50, or 78, or whatever, but this is completely irrelevant for the asymptotic complexity calculations because they are all constants. It doesn't depend on n. It's just O(1).

    O(1) = O(2) = O(c) where c is a constant.
    O(n) = O(3n) = O(cn)
    

提交回复
热议问题