Is an Algorithm Whose Time Complexity Increases with Input but with a Limit Considered O(N)?

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)?

8

1 Answer

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest