我对这个算法有疑问,我很难理解它的复杂性
While(input) {
...
array to check
...
for (int i=0; i< arraysize; i++) {
creation first subarray;
creation second subarray;
}
for (int i=0; i < firstsubarraysize; i++) {
addinput to array;
for( int j = 0; j < secondsubarraysize; j++) {
calculate array[j] * input;
}
}
}// endwhile
那么,考虑输入一个 M 变量并且数组是 N,大 O 将是 O(m(nlogn)) 或 O(n2)? 子数组并不总是 n\2。 如果我不是很清楚,我深表歉意。
最佳答案
通常,对于每个嵌套循环,时间复杂度的大 O 中会有一个 N。
这里假设最外层的循环while
运行了M次,那么里面有两个嵌套的for
。所以总数是 O(M N^2)
。
关于algorithm - 算法的大O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46761667/