Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我试图找出嵌套for循环的运行时复杂性_C++_Big O - Fatal编程技术网

C++ 我试图找出嵌套for循环的运行时复杂性

C++ 我试图找出嵌套for循环的运行时复杂性,c++,big-o,C++,Big O,所以我考虑的答案是O(log(n)),因为for嵌套的for循环x是double,y是double(log(n)*log(n))=(log^2(n)),它给出了log(n) 我考虑的第二种方法是,由于外部for循环和内部for循环都将it变量加倍,所以组合的运行时将只是O(n) 我不确定while循环是否会在“最终答案”中起作用,因为它只是添加了log(n) for(x=1;x这里有一个提示 稍微修改代码,以计算任意给定值N的内部循环迭代次数。如下所示: unsigned long test1(

所以我考虑的答案是O(log(n)),因为for嵌套的for循环x是double,y是double(log(n)*log(n))=(log^2(n)),它给出了log(n)

我考虑的第二种方法是,由于外部for循环和内部for循环都将it变量加倍,所以组合的运行时将只是O(n) 我不确定
while
循环是否会在“最终答案”中起作用,因为它只是添加了log(n)

for(x=1;x这里有一个提示

稍微修改代码,以计算任意给定值N的内部循环迭代次数。如下所示:

unsigned long test1(unsigned long n)
{
    unsigned long x, y;
    int iterations = 0;

    for (x = 1; x < n; x = x * 2) {
        for (y = 1; y < n; y = y * 2) {
            iterations++;
        }
    }

    unsigned long i = n;
    while (i > 0) {
        iterations++;
        i = i / 2;
    }
    return iterations;
}
现在让我们用Excel绘图。上面生成的sameple“x”值呈指数增长,但我们仍然用线性x轴和y轴绘图


问问你自己——这看起来像是一个
y=N
y=lg(N)
的图形吗?

log(N)*log(N)
(即
log²(N)
)是
O(log(N))
需要常数c和n0存在,使得对于任何N>n0,不等式
log(N)*log(N)非常感谢您绘制所有要点进行解释。因此,在查看图表后,它似乎是对数的。欢迎使用堆栈溢出!问一个明确的问题并没有坏处:它有助于回答问题。
unsigned long test1(unsigned long n)
{
    unsigned long x, y;
    int iterations = 0;

    for (x = 1; x < n; x = x * 2) {
        for (y = 1; y < n; y = y * 2) {
            iterations++;
        }
    }

    unsigned long i = n;
    while (i > 0) {
        iterations++;
        i = i / 2;
    }
    return iterations;
}
1,1
2,3
4,7
8,13
16,21
32,31
64,43
128,57
256,73
512,91
1024,111
2048,133
4096,157
8192,183
16384,211
32768,241
65536,273
131072,307
262144,343
524288,381
1048576,421
2097152,463
4194304,507
8388608,553
16777216,601
33554432,651
67108864,703
       n, ld(n), f(n), 2*ld²(n)

       1,     0,    1,        0
       2,     1,    3,        2
       4,     2,    7,        8
       8,     3,   13,       18
      16,     4,   21,       32
      32,     5,   31,       50
      64,     6,   43,       72
     128,     7,   57,       98
     256,     8,   73,      128
     512,     9,   91,      162
    1024,    10,  111,      200
    2048,    11,  133,      242
    4096,    12,  157,      288
    8192,    13,  183,      338
   16384,    14,  211,      392
   32768,    15,  241,      450
   65536,    16,  273,      512
  131072,    17,  307,      578
  262144,    18,  343,      648
  524288,    19,  381,      722
 1048576,    20,  421,      800
 2097152,    21,  463,      882
 4194304,    22,  507,      968
 8388608,    23,  553,     1058
16777216,    24,  601,     1152
33554432,    25,  651,     1250