for (int i = 0; i < n; i++ ) {
for (int j = 0; j < n; j++ {
Simple Statement
}
}
for (int k = 0; i < n; k++ {
Simple Statement
Simple Statement
Simple Statement
Simple Statement
Simple Statement
}
Simple Statement*25
对于嵌套循环,我发现1时间复杂度(对于int i = 0)+ N + 1(对于i n种)+ N(对于i ++)* [1(对于int J = 0)+ 1 + N(对于J&n种)+ N(其中j ++)+ N(对于简单语句)。这是(1 + N + 1 + n)个的(1 + 1 + N + N)+ N =(2 + 2n)的的(的2 + 2n)+ N = 4N ^ 2 + 9N +4
For the nested loop, I find a time complexity of 1 (for int i = 0) + n + 1 (for i < n) + n (for i++) * [ 1 (for int j = 0 ) + 1 + n ( for j < n) + n ( for j++) ] + n (for the simple statement). This is (1+n+1+n)(1+1+n+n)+n = (2 + 2n)(2+2n)+n = 4n^2 + 9n + 4.
对于下面的循环,我觉得1(对于int k = 0)+ N + 1的时间复杂度(对于i n种)+ N(对于k ++)+ 5N(为五简单语句)。这是1 + N + 1 + N + 5N = 7N + 2。在接下来的25简单的报表,我发现他们有25时间复杂度。
For the following loop, I find a time complexity of 1 (for int k = 0) + n + 1 (for i < n) + n (for k++) + 5n (for the five simple statements). This is 1+n+1+n+5n = 7n+2. For the next 25 simple statements, I find they have a time complexity of 25.
所以总的时间复杂度为4N ^ 2 + 8N + 4 + 5N + 2 + 25 = 4N ^ 2 + 16N + 31,但是,我的书上说的时间复杂度为n ^ 2 + 5N + 25
So the total time complexity is 4n^2 + 8n + 4 + 5n + 2 + 25 = 4n^2 + 16n + 31, however, my book says the time complexity is n^2 + 5n + 25.
我做了什么错?
编辑:但是现在看来,这本书是讲述的只有的简单语句的时间复杂度。我现在想我的问题是这样的(因为它是在标题):什么是算法的时间复杂度
It is now apparent that the book is telling the time complexity of only the simple statements. I suppose now my question is this (as it was in the title): What is the time complexity of the algorithm?
您的书是计数的只有的数量 SimpleStatements
执行。
Your book is counting only the number of SimpleStatements
executed.