当前位置:网站首页>【时间复杂度和空间复杂度】
【时间复杂度和空间复杂度】
2022-06-26 15:47:00 【@slow_walker】
时间复杂度和空间复杂度
复杂度分析
衡量不同算法的优劣,主要还是根据算法所占的空间和时间两个维度去考虑。但是,世界上不会存在完美的代码,既不消耗最多的时间,也不占用最多的空间,鱼和熊掌不可得兼,那么我们就需要从中去寻找一个平衡点,使得写出一份较为完美的代码。
时间复杂度
时间复杂度:分析算法的执行的效率
常见的时间复杂度
O(1) < O(nlogn)<O(n) < O(n2) < O(2^n) < O(n!)
1.O(1)
int fun(int n)
{
int i = n;
int j = 3*n;
return i+j;
}
2.O(nlogn)
int fun(int n)
{
int i = 1;
while(i<= n)
{
i = i*2;
}
return i;
}
3.O(n)
int fun(int n)
{
sum = 0;
for(int i = 0;i<n;i++)
{
sum += i;
}
return sum;
}
4.O(mlogn)
int fun(int m,int n)
{
sum = 0;
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
sum+= i*j;
j = j*2;
}
}
return sum;
}
5.O(n2)
int fun(int n)
{
sum = 0;
for(int i = 0;i<n;i++)
{
for(int j =0;j<n;j++)
{
ssum+= i*j;
}
}
return sum;
}
空间复杂度
算法所占内存空间的大小:看变量语句是不占空间的
常见的空间复杂度O(1) <O(n) < O(n2)
1.O(1)
int fun(int n)
{
sum = 0;
for(int i = 0;i<n;i++)
{
sum += i;
}
return sum;
}
2.O(n)
int fun(int n)
{
int arr[N];
int i = 0;
while(i<= N)
{
i = i*2;
}
return i;
}
3.O(MN)
int fun(int m,int n)
{
int arr[M][N];
for(int i = 0;i<m;i++)
{
for(int j = 0;j>=n;j++)
{
sum += arr[i][j];
}
}
return sum;
}
边栏推荐
- C. Inversion Graph
- How to identify contractual issues
- IntelliJ idea -- Method for formatting SQL files
- The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation
- 8 user defined evaluation function
- SVG大写字母A动画js特效
- 2 three modeling methods
- Angel 3.2.0 new version released! Figure the computing power is strengthened again
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- 【问题解决】新版webots纹理等资源文件加载/下载时间过长
猜你喜欢

JVM笔记

Svg rising Color Bubble animation
![[applet practice series] Introduction to the registration life cycle of the applet framework page](/img/82/5a9219a7c2ee211180cc4b2d138204.png)
[applet practice series] Introduction to the registration life cycle of the applet framework page

Summary of data interface API used in word search and translation applications

(一)keras手写数字体识别并识别自己写的数字

2 三种建模方式

简单科普Ethereum的Transaction Input Data

SVG大写字母A动画js特效

9 Tensorboard的使用

Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
随机推荐
STEPN 新手入門及進階
Transaction input data of Ethereum
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
Svg savage animation code
JS text scrolling scattered animation JS special effect
Reflection modification final
[CEPH] Lock Notes of cephfs
PCIe Capabilities List
2 three modeling methods
5 模型保存与加载
R语言使用cor函数计算相关性矩阵进行相关性分析,使用corrgram包可视化相关性矩阵、行和列使用主成分分析重新排序、下三角形中使用平滑的拟合线和置信椭圆,上三角形中使用散点图、对角线最小值和最大值
OpenSea上如何创建自己的NFT(Polygon)
「幹貨」NFT 上中下遊產業鏈全景分析
IntelliJ idea -- Method for formatting SQL files
Is it safe to open an account for mobile stock registration? Is there any risk?
Ansible自动化的运用
Solana扩容机制分析(1):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT
nanoPi Duo2连接wifi
Audio and video learning (II) -- frame rate, code stream and resolution