What is the complexity of this code?
public class test5{
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
for (int i = 1; i<=
You are correct, a tight upper asymptotic bound for both the first and second nested loop blocks—say T_A(n)
and T_B(n)
, respectively—is O(n^2)
, and hence the function as a whole runs as O(n^2)
, asymptotically.
You can analyze this in detail using Sigma notation to count the number of basic operations in the inner loop blocks for each of the nested loop blocks T_A(n)
and T_B(n)
:
Where we've treated the System.out.print ("*");
operation as basic operation.