-1

Need help how to calculate the analysis of an algorithm

I understand the concept of Big O and know the difference between the types of cases there are.

public int examplecode(int[] data)
{
    int n = 0;
    int counter = data[0];

    for(int j = 1; j < n; j++)
    {
        if(data[j] > counter)
            counter++;
    }

    if(data[j] > counter)
        counter++;

    for(int j = 1; j < n; j++)
    {
        for(int k = 1; k < n; k++)
        {
            if(data[k] > counter)
                counter++;
            counter += 3;
        }
    }

    return n;
}

I expect n^2 + 9n

Alan Sereb
  • 2,358
  • 2
  • 17
  • 31
NotSwing
  • 9
  • 2

1 Answers1

2

With the syntax errors corrected, that code runs in O(1), since neither of the loops actually iterates. Since n is not a parameter of the function, though, saying that it's "n^2 + 9n" is meaningless. So your conclusion is neither true nor false.

Sneftel
  • 40,271
  • 12
  • 71
  • 104