当前位置:网站首页>大O记法解释
大O记法解释
2022-06-13 09:25:00 【edui】
在比较算法的性能时,不仅仅和相同数据项时快慢和所用空间有关,更重要的应该是数据量变化时随数据项变化的变化程度,大O记发即为算法的时间复杂度和空间复杂度随数据项饿变化关系曲线,由于多项式曲线的形状通常由最高次项决定,当数据量比较高时低次项的影响相对于最高次项很小为了方便可以忽略。
- 时间复杂度:
简单的说时间复杂度就是随着N的增加,程序需要运行的次数随N的变化曲线。算法中某个函数有n次基本操作重复执行,用T(n)表示,现在有某个辅助函数f(n),使得当n趋近于无穷大时,T(n)/f(n)的极限值为不等于零的常数,则称f(n)是T(n)的同数量级函数。记作T(n)=O(f(n)),称O(f(n)) 为算法的渐进时间复杂度,简称时间复杂度
举个简单栗子:
sum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
sum++;
第一行频度1,第二行n,第三行n²,第四行n²,T(n)=2n²+n+1 =O(n²)时间复杂度为
O(n²)
- 空间复杂度:
一个算法的空间复杂度定义为该算法所耗费的存储空间,储存空间的消耗是由函数中创建对象的个数决定,即空间复杂度是看函数中创建对象的个数和时间复杂度计算方法类似。在递归时每递归总用空间和N的近似曲线关系。
例如:
int fun(int n)
{
int num = 1;
if(n <= num )
return 1;
else
return n*fun(n-1);
}
递归空间复杂度:
O(n)=n*1=n;
递归次数为n次,每次进入创建1个对象
总结,大O记法优势就是简单,易懂,但是缺点就是过于组略,比如O(n)和O(5n)是一样的同样记作O(n),所以当数据量相同时显然O(n)更快些,认识到这点在算法分析时更加准确。
边栏推荐
- Musk's "meta universe" dream
- LeetCode 343. 整数拆分
- LeetCode 6097. Match after replacing characters (Dictionary)
- Classes and objects - initialization and cleanup of objects
- 7-3 virus traceability (20 points)
- (bfs+GOOD) acwing 845. Eight digit
- turtle库的使用数字时钟模拟时钟动态显示
- LeetCode 6098. Count the number of subarrays whose score is less than K (prefix and + binary search)
- 说说MySQL索引机制
- Queue and stack
猜你喜欢

turtle库显示系统时间

UNIX Environment advanced programming --8- process control ---8.5 function exit-8.6 function wait and waitpid
![[51nod p2527] or and sum [bit operation]](/img/50/75ee9ee40ef0ca9b99e6900018b61a.jpg)
[51nod p2527] or and sum [bit operation]

(state compression dp+good) acwing 291 Mondrian's dream
![[Luogu p1090, ssl1040] merged fruit [pile]](/img/65/2206dead12cc61fed9ff582acb34c0.jpg)
[Luogu p1090, ssl1040] merged fruit [pile]

Dynamic display of analog clock using digital clock in turtle Library

VGA common resolution and calculation method

acwing 786. Number k

(bfs+GOOD) acwing 845. Eight digit

【pytorch环境安装搭建】
随机推荐
[51nod 3062] n queen problem V2 [bit operation DFS]
Dynamic display of analog clock using digital clock in turtle Library
MySQL中redo日志和undo日志简述
C language: summary of question brushing (1)
Learning makefile with me
【动态规划】入门篇
[51nod p3111] xiaoming'ai intercepts [Las]
云计算企业崛起 甲骨文数据库市场主导地位动摇
Summary of string, vector and array learning
[51nod 2493] sum of binary distances [bit operation]
List list
[Luogu p1403] Research on divisor
[implementation of depth first search]
[pytorch environment installation]
Jenkins接入Openldap用户认证
LeetCode 5289. Fair distribution of cookies (DFS)
SpEL表达式 简单使用
LeetCode 343. 整数拆分
Jenkins接入Openldap用戶認證
LeetCode 583. Deleting two strings