2012-10-15 16 views
4

所以我正在回答我的一个计算类的问题。我开发了一个算法,然后它问我算法的复杂性。我现在不太擅长确定复杂性,所以任何人都可以验证?Big(O)符号:任何人都可以验证吗?

的代码如下:

if(A.type is not Comparable): return False     // Max runs = 1 
current ← A.head            // Max runs = 1 
printedFirst ← False           // Max runs = 1 
while(current.hasNext):          // Max runs = s-1 
    if (current.value < current.next.value):     // Max runs = s-1 
     if (printedFirst): print “, “      // Max runs = s-1 
     print “(“ + current.value + “, “ + current.next.value + “)” //runs = s-1 
     printedFirst ← True         // Max runs = s-1 
    current = current.next          // Max runs = s-1 

因此,我们必须

3(1) + 6(s - 1) = 3 + 6s - 6 = 6s - 3 = O(n) 

正确?

+2

你是不是在改变里面的“我”?这将使它成为一个无限循环。 – RBarryYoung

+0

好吧,它可能会崩溃..;) –

+0

哈哈..是否仍然使它O(N)? =)我已经更新了算法 – connorbode

回答

5

一个单一的循环,只有一个,如果内部...去O(n)。

祝你的课程好运。

+0

Cheers bud =) - – connorbode