gpt4 book ai didi

algorithm - 大(O)表示法 : Can anyone verify?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:55:59 24 4
gpt4 key购买 nike

所以我要回答我的一门计算类(class)的问题。我开发了一个算法,然后它问我算法的复杂度。我目前不太擅长确定复杂性,所以任何人都可以验证吗?

代码如下:

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 )

正确吗?

最佳答案

一个单独的 while 循环,里面只有一个 if ......去寻找 O(n)。

祝你上课顺利。

关于algorithm - 大(O)表示法 : Can anyone verify?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com