计算和分析运行时

问题描述 投票:0回答:1
public static long Compute(int x){
    long count = 0;
    if(x==1)
        return count=1;
    else if(x==3)
        for(int i =0;i<x;i++){
            count+=i;
    }
    else if (x==5)
            for(int j=0;j<x;j++)
                for(int k=0;j<=x;k++)
                     return +=(i*j);
                }
            }
    return result;
}

我想了解如何解决每一个语句来制定运行时的T(n)。我有2个if语句和一个for-loop和一个嵌套for-loop。我的答案是O(n^2)。

algorithm time-complexity big-o analysis
1个回答
1
投票

f = O(g)意味着有一个N和c,所以n>N意味着f(n) <= c * g(n)。我假设f和g是非负数)。

该代码需要x>5的恒定时间。所以是O(1)。

© www.soinside.com 2019 - 2024. All rights reserved.