当前位置:网站首页>大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)更快些,认识到这点在算法分析时更加准确。
边栏推荐
- Tree and binary tree: application of binary tree traversal
- LeetCode 6095. Strong password checker II
- Pxxx local socket communication
- [pytorch environment installation]
- C language: file operation
- [51nod 3062] n queen problem V2 [bit operation DFS]
- Simple use of spiel expressions
- 【20220526】UE5.0.2 release d11782b
- Calculate the number of days between two times (supports cross month and cross year)
- A static variable is associated with a class and can be used as long as the class is in memory (the variable does not exist as long as your application terminates). (heap body, stack reference)
猜你喜欢
(dp+ memory) acwing 901 skiing
Learning makefile with me
The rise of cloud computing enterprises and the shaking of Oracle database market dominance
Jenkins接入Openldap用户认证
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
turtle库显示系统时间
Jenkins accédant à l'authentification de l'utilisateur openldap
Tree and binary tree: basic operation and implementation of binary tree
acwing 790. The third root of a number (dichotomy)
[51nod p3058] Xiao ming'ai set [set]
随机推荐
Class and object -- friend
Simple use of spiel expressions
C language: dynamic memory management
谨记! 写代码别太自信! 一定要写点关键日志info输出,不然问题都定位不到。
云计算企业崛起 甲骨文数据库市场主导地位动摇
[51nod p3216] Award [bit operation]
acwing 786. Number k
Classes and objects -- polymorphic
I have summarized the knowledge points of JS [intermediate and advanced] for you
攻防世界-PWN-shell
英国出台粮食安全计划抵御粮食供应危机
[51nod p3395] n-bit gray code [bit operation]
Figure: concept of figure
Trees and binary trees: the concept of binary trees
【20220526】UE5.0.2 release d11782b
[51nod p3047] displacement operation [bit operation]
虚拟化和云计算文章大合集
(dfs) acwing 842. Arrange numbers
微信小程序客服自动回复——PHP实现
Timestamp to localdate