Big o 这段代码的时间复杂度很大
给定以下代码-:Big o 这段代码的时间复杂度很大,big-o,time-complexity,pseudocode,Big O,Time Complexity,Pseudocode,给定以下代码-: for(int i=1;ii的每个值的内部循环的总迭代次数将为 i = 1: j = 1, 2, 3 ..., n ---> total iterations = n i = 2: j = 1, 3, 5 ..., n ---> total iterations = n/2 if 2 divides n or one less otherwise i = 3: j = 1, 4, 7 ..., n ---> total iterations = n/3 if
for(int i=1;ii的每个值的内部循环的总迭代次数将为
i = 1: j = 1, 2, 3 ..., n ---> total iterations = n
i = 2: j = 1, 3, 5 ..., n ---> total iterations = n/2 if 2 divides n or one less otherwise
i = 3: j = 1, 4, 7 ..., n ---> total iterations = n/3 if 3 divides n or one less otherwise
...
i = m: j = 1, 1 + m, ... , n ---> total iterations ~ n/m
...
1
因此,大约总迭代次数为(n+n/2+n/3…+1)
该和是数值约为ln(n)+C的值,因此总迭代次数约为nln(n)
,并且由于所有对数都由一个常数关联,因此迭代次数将为O(nlogn)
您可能会喜欢使用我在我的答案中使用的方法:但这需要时间。@Alp j随着每次迭代的增加而增加,而不是1。不确定,但它可能会有帮助:下面是@GrijeshChauhan:这个例子很好,但我正在寻找一个合适的数学证明,在那里我会将日志
应用于基数2。也许我正在考虑迭代重现rrence解算,显然不能在这里应用。这正是我要找的。谢谢!@TheRedBlackTree你是对的,n(n+…+1)太多了。谢谢你抓住了!