What is the complexity of this function with nested loops?

后端 未结 1 1519
迷失自我
迷失自我 2021-01-28 14:02

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<=         


        
相关标签:
1条回答
  • 2021-01-28 14:48

    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.

    0 讨论(0)
提交回复
热议问题