当前位置:网站首页>[time complexity and space complexity]
[time complexity and space complexity]
2022-06-26 16:02:00 【@slow_ walker】
Time complexity and space complexity
Complexity analysis
Measure the advantages and disadvantages of different algorithms , It is mainly considered according to the space and time occupied by the algorithm . however , There will be no perfect code in the world , Neither consumes the most time , It doesn't take up the most space , You can't have both fish and bear paws , Then we need to find a balance , Make it possible to write a more perfect code .
Time complexity
Time complexity : Analyze the efficiency of algorithm execution
Common time complexity
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;
}
Spatial complexity
Size of memory space occupied by algorithm : The variable statement does not occupy space
Common spatial complexity 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;
}
边栏推荐
- Swiftui retrieves the missing list view animation
- Stepn novice introduction and advanced
- selenium chrome 禁用js 禁用图片
- svg野人动画代码
- svg上升的彩色气泡动画
- Solana扩容机制分析(1):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
- 5000 word analysis: the way of container security attack and defense in actual combat scenarios
- NFT Platform Security Guide (1)
- (1) Keras handwritten numeral recognition and recognition of self written numbers
- Selenium saves elements as pictures
猜你喜欢
随机推荐
Interview pit summary I
TweenMax+SVG切换颜色动画场景
Nanopi duo2 connection WiFi
还存在过有键盘的kindle?
【leetcode】48. Rotate image
Common properties of XOR and addition
NFT Platform Security Guide (2)
How to create your own NFT (polygon) on opensea
svg canvas画布拖拽
Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
今年高考英语AI得分134,复旦武大校友这项研究有点意思
R语言使用cor函数计算相关性矩阵进行相关性分析,使用corrgram包可视化相关性矩阵、行和列使用主成分分析重新排序、下三角形中使用平滑的拟合线和置信椭圆,上三角形中使用散点图、对角线最小值和最大值
首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...
反射修改final
El dialog drag and drop, the boundary problem is completely corrected, and the bug of the online version is fixed
Audio and video learning (II) -- frame rate, code stream and resolution
selenium chrome 禁用js 禁用图片
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example
svg环绕地球动画js特效
Simple use of tensor









