分析时间我的程序复杂 [英] analysing time complexity of my programs
问题描述
我在确定时间的算法复杂度有问题。
I have problem in determining time complexities of algorithms.
for(int i=0;i <n i++){} O(n)
for(int i= 0 ;i<n ;i++){ O(n^2)
for(int j=0;j<n;j++){
}
}
现在用于以下code什么的复杂性
Now for following code whats the complexity
for(i =0; i<n ; i++) {}
for (j=0;j<n ;j++ ) {}
是O(2N),因为它invloves 2个独立的循环?
is it O(2n) as it invloves 2 seperate loops?
如果我开始J = 5到n?
what if i start j =5 to n?
推荐答案
没有 O(2N)
,它只是 O(N)
。换句话说,它缩放以同样的速度为 N
增大。
There is no O(2n)
, it's just O(n)
. In other words, it scales at the same rate as n
increases.
如果这是一个的嵌套的循环,这将是为O(n 2 )
但是$ P $你的 {}
空块psence意味着它没有嵌套。
If it was a nested loop, it would be O(n2)
but the presence of your {}
empty blocks means it isn't nested.
和它没有什么区别,你是否开始1至5个,但仍然与秤ñ
,只是一个轻微的负持续增加。因此,仍然 O(N)
。
And it makes no difference whether you start at one or five, it still scales with n
, just with a slightly negative constant addition. Hence still O(n)
.
的复杂性 O(N)
, O(CN)
和为O(n + C)
(其中 C
为常数)都是等价的。此外,您还通常只使用具有最高效力的期限。
The complexities O(n)
, O(cn)
and O(n+c)
(where c
is a constant) are all equivalent. In addition, you also generally only use the term with the highest effect.
所以,你通常不会看到 0(7N 3 + 3N 2 + 12N + 2)
,这将简化为为O(n 3 )
。
So you won't usually see O(7n3 + 3n2 + 12n + 2)
, that will be simplified to O(n3)
.
这篇关于分析时间我的程序复杂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!