给予大角色:
for (int i = 0; i < n; i++) {
if (i * i < n) {
for (int j = 0; j < n; j++) {
count++;
}
}
else {
int k = i;
while (k > 0) {
count++;
k = k / 2;
}
}
}
所以这就是我的想法......不过确定它是否正确:
第一个for循环将运行n次迭代。然后第一个for循环中的for for循环也将运行n次迭代,给出O(n ^ 2)。
对于else语句,while循环将运行n次迭代,k = k / 2将运行logn time给出O(nlogn)。那么整个事情看起来就像是n ^ 2 + nlogn并且通过采用更长的运行时间,答案将是theta n ^ 2?
答案 0 :(得分:0)
我想结果是O(nlogn)
,因为对于线性n,i*i
通常不小于n。其他分支将占主导地位。
示例:
n= 10000
after i=100 the else part will be calculated instead of the inner for loop