伪代码 您能帮我找出算法的复杂性吗,因为这些j <-2j;我<-i + 1让我有点生气。
答案 0 :(得分:1)
让我们首先考虑内部循环:
j = 1
while j<=n
O(1)
j = 2*j
此循环针对j = 1,2,4,8,...
期间j<=n
的值运行。
因此,此循环将具有对数时间复杂度,即O(log n)
。
现在考虑外部循环:
i = 2
while i<=n
O(1)
//inner loop
i = i+1
此循环运行i = 2,3,,4,...,n
的值
因此,外循环具有线性时间复杂度,即O(n)
。
总时间复杂度= O(n)*O(log n) = O(n*log n)