简单算法复杂度计算

1 void reverse( string &s ){
2     int n = s.size();
3     for(int i = 0 ; i < n/2 ; i++ )//这个循环走了n/2次,所以O(n)
4         swap( s[i] , s[n-1-i] );
5 }
1 int count = 1;      
2 while (count < n){//cout*2^x=n,x=logn,所以O(logn)
3    count = count * 2;
4 }
1 int i, j;      
2 for(i = 1; i < n; i++)//n^3
3     for(j = 1; j < n; j++)
4         for(j = 1; j < n; j++){
5                ......
6         }
1 int i, j;      
2 for(i = 0; i < n; i++){//n^2
3     for(j = i; j < n; j++){   
4         .....
5     }
6 }
1 for(int i=2;i*i<=n;i++){//sqrt(n)
2     ....
3 }

 

posted @ 2019-08-08 21:22  acwarming  阅读(179)  评论(0编辑  收藏  举报