Home > Mobile >  Is an algorithm whose time complexity increases with input but with a limit considered O(n)?
Is an algorithm whose time complexity increases with input but with a limit considered O(n)?

Time:07-19

if a function's statements execution increases with input but with a limit, would it be considered O(n) or O(1)?

for example:

void func(int n)
    {
        if (n > 1000)
        {
            for (int i = 0; i < 1000; i  )
            {
                //do thing
            }
        }
        else
        {
            for (int i = 0; i < n; i  )
            {
                //do same thing
            }
        }
    }

is this function O(n) or O(1)?

CodePudding user response:

It is O(1), not O(n).

Big-O analysis is asymptotic: it intentionally ignores an arbitrarily large initial section of the performance function, in order to accurately describe the large-scale behavior.

  • Related