当我开始研究复杂性的时候,我纠结于这个问题:
void what(int n) {
int i;
for (i = 1; i <= n; i++) {
int x = n;
while (x > 0)
x -= i;
}
}
Well, the first for loop is clearly O(n)
. The first iteration is O(n)
, second one is O(n/2)
.. and like that log(n)
times I guess?
也就是 O(n) * O(log(n)) = O(n * log(n)) complexity
我没说错吧?
编辑: (不是复制品)我知道大 O 是什么。我已经问过特定情况下的正确评估。