Hello, I have been having a tough time figuring a function of n for a snippet of code. I honestly don't know how to tackle this particular problem.

Assuming 'n' is the length of the array, how often is the if statement executed?

public sortArray(int[] array) {

for (int i = 0; i < array.length; i++) {

int j = i;

while (j > 0) {

if (array[j] < array[j-1]) {

int temp = array[j];

array[j] = array[j-1];

array[j-1] = temp;

}

j--;

}

}

}

Doesn't the number of times the if statement runs vary based upon the values in the array? Wouldn't this be "random"?

If anyone could walk me through deriving the answer, it would be very helpful.

Assuming 'n' is the length of the array, how often is the if statement executed?

public sortArray(int[] array) {

for (int i = 0; i < array.length; i++) {

int j = i;

while (j > 0) {

if (array[j] < array[j-1]) {

int temp = array[j];

array[j] = array[j-1];

array[j-1] = temp;

}

j--;

}

}

}

Doesn't the number of times the if statement runs vary based upon the values in the array? Wouldn't this be "random"?

If anyone could walk me through deriving the answer, it would be very helpful.