看到网上一些资料的案例不全,所以自己开个来复习。
O(1)<O(log2n)<O(n)<O(nlog2n)<O(n^2)<O(n^3)<…<O(2^n)<O(n!)
O(1)
int a = 1, b = 3, c = 3;
O(log2n)
int i = 1; while(i<=n) { i*=2 }
O(n)
int sum = 0; for(int i = 0; i < n; i++) { sum+=i; }
O(nlog2n)
for(int m=1;m<n;m++) { i=1; while(i<n) { i=i*2; } }
O(n^2)
int num1, num2; for(int i=0; i<n; i++) { num1 += 1; for(int j=1; j<=n; j++) { num2 += j; } }
O(n^3)
int num1, num2, num3; for(int i=0; i<n; i++) { num1 += 1; for(int j=1; j<=n; j++) { num2 += j; for(int k=1; k<=n; k++) { num3 += k; } } }
O(2^n)
2^n = 2*2*2*........2* // 有n个2
O(n!)
n! = 1*2*3*4*5......*n;
相关文章: https://blog.csdn.net/u012925946/article/details/84030160
https://blog.csdn.net/qq_41672557/article/details/101699850
https://blog.csdn.net/qq_40513633/article/details/107705801