-4

Example1:

This I would say is O(log(n)), my reasoning is that if we choose a test n, n = 10, then 'i' would run: 0,2,4,6,8,10, so 'i' is behaving linearly not growing but just adding + 2 each time it iterates. And the method is also 'n' - dependent meaning that as n grows so does the method. So the first loop is O(n), then the second loop goes, if we choose n test to be n = 10, then after each iteration 'j' would run: 2,4,8,16,32,64 then can be thought of as 2^n which is a logarithmic function, so this loop is logarithmic. So computing this: O(n)*O(log(n)) = O(log(n))

for (i = 0; i < n; i = i + 2) {
for ( i = 1; j <= n; j = j*2) {
System.out.println("Hi");
}
}

Example2:

for this one if we choose n to be n = 10, then 'i' runs: 2,4,8,16,32 again can be rewritten as 2^n, this is logarithmic, O(log(n))

for (i = 1; i < n; i = i + i) {
System.out.println("Soda");
}

Example3:

for this one the first loop is n-dependent, the method changes as n grows, nothing fancy with 'i' it simply adds +1 so this is a n-dependent loop of O(n) for any inputted n. if the number is even it runs the first nested loop, if the number is positive it runs the second nested loop. The first inner loop is the same complexity, O(n). and the last inner loop is (I think, despite n*n) O(n). So computing this we get: O(n)*O(n) = O(n^2) if the if-statement is true, and O(n)*O(n) if the if statement is false, so in the worst case it is O(n^2) because we multiply the two O(n)'s.

for (i = 0; i < n; i++) {
if (i % 2 == 0) {
for (j = 0; j < n; j++) {
System.out.println("Rawr");
}
} else {
for (j = 0; j < n*n; j++) {
System.out.println("Computer");
}
}

Have I made errors? please explain Thank you

yre
  • 285
  • 4
  • 12
  • Is your question 'check my CS homework'? If so, it's probably not a good fit for SO. Take a look at https://stackoverflow.com/help/on-topic – pvg Apr 11 '17 at 17:49
  • Possible duplicate of [Big O, how do you calculate/approximate it?](http://stackoverflow.com/questions/3255/big-o-how-do-you-calculate-approximate-it) – Paul Hankin Apr 11 '17 at 17:54
  • Also, this isn't at all related to Java, it's a general programming question. – M. Prokhorov Apr 11 '17 at 18:00
  • " O(n)*O(log(n)) = O(log(n))" ...no? It's O(n * log(n)). – Louis Wasserman Apr 11 '17 at 20:14
  • @LouisWasserman Ah right! I was wondering about that so whenever we have a linear function and a log function nested or vice versa we multiply them together to get O(n*log(n)). And for that matter all times we have nested loops things will get multiped. Thanks! – yre Apr 11 '17 at 21:31

1 Answers1

0

Unfortunately, from the info you are giving, not much can be told. Are you looking for time complexity? spacetime complexity? runtime complexity?

In addition to this, the complexity of methods is actually only to be useful if the method is called more than once, which doesn't happen as you propose. If you don't know how the method is called, you will not be able to determine the complexity(either one of them) accurately.