-3

Here is a part of an algorithm to rotate an array by 180 degrees.

System.out.println("\nHasil:");
for (i= arr.length-1; i>=0; i--) {
    System.out.print("[");
    for (j= arr.length-1; j>=0; j--){
        System.out.print(arr[i][j]);
        if (j != 0) {
            System.out.print(",");
        }
    }System.out.println("]");
}

I'm confused how to determine the big o notation that have nested loop with if statement inside it.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    `n^2` since the inner and outer loop both go through 0 to `arr.length`. the if statement does not matter. – luk2302 Oct 10 '21 at 14:54

1 Answers1

0

The if conditional is not a factor in big O notation of runtime. The length of the array is the variable here (that will be ā€˜n’) and I see you have a nested loop, 2 loops, that will both traverse the length of that array. That means the runtime will be on the order of n * n, or n squared. O(n squared). It can also be written as O(n^2)

Michael K
  • 1,031
  • 2
  • 14
  • 27