参考视频:https://www.bilibili.com/video/BV14j411f7DJ
目录
1.常数阶O(1)
2.对数阶O(IogN)
3.线性阶O(n)
4.线性对数阶O(nlogN)
5.平方阶O(n^2)
6.立方阶O(n^3)
7.K次方阶O(n^k)
8.指数阶(2^n)
9.阶乘O(n!)
两层for循环
for (int i = 1; i <= n; i++){
for (int j = 1; j <=n; j++){
×++;
}
}
for (int i = 1; i<= n; i++){
x++;
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <=n; j++){
×++;
}
}
1.常数阶O(1)
int x = O;
int y = 1;
int temp = x;
x=y;
y = temp;
2.对数阶O(IogN)
int i= 1;
while(i< n){
i = i * 2;
}
循环次数k
2^k=n
k=logn
3.线性阶O(n)
for (int i = 1; i<= n; i++){
x++;
}
4.线性对数阶O(nlogN)
for(int i= O; i <= n; i++){
int x = 1;
while(x < n){
× = x * 2;
}
}
5.平方阶O(n^2)
for (int i = 1; i <= n; i++){
for (int j = 1; j <=m; j++){
×++;
}
}
// O(n*m)
6.立方阶O(n^3)
for (int i = 1; i <= n; i++){
for (int j = 1; j <=n; j++){
for (int k = 1; k <=n; k++){
×++;
}
}
}