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 use of lorem ipsum text began with Web design.

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

Computer Science & Information Technology

The portion of a disk or drive that contains the MBR is known as the:

A. boot sector B. primary partition C. extended partition D. boot partition

Computer Science & Information Technology

Java is a(n) ____________________ language, which encourages good software engineering practices such as code reuse.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

What information is included in a BPS?

What will be an ideal response?

Computer Science & Information Technology