1.
Q-4: Given the following code fragment, what is its Big O running time? (Presume that variable
n
has been declared elsewhere in the program.)int test = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
test = test + i * j;
}
}
- O(n)
- In an example like this you want to count the nested loops. especially the loops that are dependent on the same variable, in this case, n.
- O(n^2)
- A singly nested loop like this is O(n^2)
- O(log n)
- log n typically is indicated when the problem is iteratvely made smaller
- O(n^3)
- In an example like this you want to count the nested loops. especially the loops that are dependent on the same variable, in this case, n.