Determining Big O Notation

前端 未结 5 2154
后悔当初
后悔当初 2020-12-17 22:50

I need help understanding/doing Big O Notation. I understand the purpose of it, I just don\'t know how to \"determine the complexity given a piece of code\".

5条回答
  •  误落风尘
    2020-12-17 23:40

    Assuming you don't count the cout as part of your Big-O measurement.

    a) O(1) you can perform the integer assignment in constant time.
    b) O(n) because it takes n operations for the loop.
    c) O(n - c) = O(n) constants disappear in Big-O.
    d.1) O(2*n) = O(n) two linear time algorithms end up being linear time.
    d.2) If n grows with pow(2, n) = 2^n, then the number of operations are O(2^n); however if n is constant it would grow with O(k), where k = 2^6 = 64, which would be linear.

提交回复
热议问题