What is the Big-O for the following function?


void algo(int n, int x) {
for (int k = 0; k < n; ++k)
if (x < 99) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < i; ++j)
System.out.println(”x = ” + x);
} else {
System.out.println(”x = ” + x);
}
}

a. O(n^3)
b. O(n log n)
c. O(n)
d. O(n^2)

a. O(n^3)
The two arrays are traversed by the parameter n, and the outer loop traverses the array from I to n; so O(n) * O(n^2), giving us O(n^3).

Computer Science & Information Technology

You might also like to view...

The default authentication option for external trusts is domain-wide authentication

Indicate whether the statement is true or false

Computer Science & Information Technology

An "if" statement is a selection statement.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology