6

What is the complexity given for the following problem is O(n). Shouldn't it be O(n^2)? That is because the outer loop is O(n) and inner is also O(n), therefore n*n = O(n^2)?

The answer sheet of this question states that the answer is O(n). How is that possible?

public static void q1d(int n) {
    int count = 0;
    for (int i = 0; i < n; i++) {
        count++;
        for (int j = 0; j < n; j++) {
            count++;
        }
    }
}

The complexity for the following problem is O(n^2), how can you obtain that? Can someone please elaborate?

public static void q1E(int n) {
    int count = 0;
    for (int i = 0; i < n; i++) {
        count++;
        for (int j = 0; j < n/2; j++) {
            count++;
        }
    }
}

Thanks

3
  • I think upper one is O(n^2).and what doubt you have about second one? Commented Jan 17, 2012 at 5:27
  • This means the answer provided by my professor is incorrect hmm. Commented Jan 17, 2012 at 5:29
  • 2
    it seems that answer sheet contains errors. Commented Jan 17, 2012 at 5:30

5 Answers 5

15

The first example is O(n^2), so it seems they've made a mistake. To calculate (informally) the second example, we can do n * (n/2) = (n^2)/2 = O(n^2). If this doesn't make sense, you need to go and brush up what the meaning of something being O(n^k) is.

Sign up to request clarification or add additional context in comments.

Comments

6

The complexity of both code is O(n*n)

FIRST

The outer loop runs n times and the inner loop varies from 0 to n-1 times

so

total = 1 + 2 + 3 + 4 ... + n

which if you add the arithmetic progression is n * ( n + 1 ) / 2 is O(n*n)

SECOND

The outer loop runs n times and the inner loop varies from 0 to n-1/2 times

so

total = 1 + 1/2 + 3/2 + 4/2 ... + n/2

which if you add the arithmetic progression is n * ( n + 1 ) / 4 is also O(n*n)

2 Comments

So if n/2 does it still equal to o(n)? But the iteration for inner loop is half the outer loop does that make any difference?
@user1085135: O(n) has the meaning linear. It doesn't matter what constant it multiplied by, what only matters is that it is linear
2

First case is definitely O(n^2)

The second is O(n^2) as well because you omit constants when calculate big O

Comments

0

Your answer sheet is wrong, the first algorithm is clearly O(n^2).

Big-Oh notation is "worst case" so when calculating the Big-Oh value, we generally ignore multiplications / divisions by constants.

That being said, your second example is also O(n^2) in the worst case because, although the inner loop is "only" 1/2 n, the n is the clear bounding factor. In practice the second algorithm will be less than O(n^2) operations -- but Big-Oh is intended to be a "worst case" (ie. maximal bounding) measurement, so the exact number of operations is ignored in favor of focusing on how the algorithm behaves as n approaches infinity.

5 Comments

The second algorithm won't be less than O(n^2), it is precisely O(n^2). I suspect you aren't quite clear on what "O(n^2)" actually means. It means that as n approaches infinity, the run time increases for the algorithm for two values of n is proportional to the difference between those n values squared.
No. In practice, the actual time complexity of the algorithm will be slightly less than O(n^2). However, because Big-Oh is a maximal bound, we say it is O(n^2). You are correct that the n/2 factor is clearly bounded by n -- as I indicated in my answer. There are other bounding measurements (ie. Big-Theta) that compute the bounding factor differently.
Why do you think the actual time complexity of the algorithm will be less than O(n^2)? I claim it will be precisely O(n^2) because the algorithm's complexity precisely matches the definition of O(n^2). If it's not exactly O(n^2), then nothing is. We say it is O(n^2) because it is O(n^2). You seem to want to make this fuzzy, confusing, and imprecise when it is absolutely, crystal clear.
Because the actual complexity is O(n^2) + C, where C is some constant. In this case C is negative. So it will be slightly less than straight n^2. The C is insignificant, but you still should write the "answer" as O(n^2) - C
A complexity of O(n^2) + C means precisely the same thing as a complexity of O(n^2).
0

Both are O(n^2). Your answer is wrong. Or you may have written the question incorrectly.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.