当前位置:网站首页>大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)更快些,认识到这点在算法分析时更加准确。
边栏推荐
- LeetCode 6098. 统计得分小于 K 的子数组数目(前缀和+二分查找)
- 【pytorch环境安装搭建】
- MySQL中redo日志和undo日志简述
- Z字形变换
- Trees and binary trees: traversal of binary trees
- @Value不生效及extend/implement其他类无法注入bean的手动注入
- (dfs+ pruning + checkerboard problem +dood) acwing 843 N-queen problem
- 英国出台粮食安全计划抵御粮食供应危机
- Exercise 8-3 rotate the array to the right (20 points)
- Tree and binary tree: operation and storage structure of tree
猜你喜欢

全新BMW i3的操控,能符合对3系之名产品的期待吗?

Classes and objects -- polymorphic

说说MySQL索引机制
![1-2 24:00 (20 points) [CSP certification true question]](/img/3b/fe2c0e46dca604e5906d9c5ceabbe3.jpg)
1-2 24:00 (20 points) [CSP certification true question]

(bfs) acwing 844. Labyrinth

Trees and binary trees: the concept of binary trees

acwing 788. Number of pairs in reverse order

C language: dynamic memory management

(dfs+ tree DP) acwing 846 Center of gravity of tree

(bfs) acwing 847. Hierarchy of points in the graph
随机推荐
[51nod 2493] sum of binary distances [bit operation]
隐私计算FATE-核心概念与单机部署
Z字形变换
[51nod p3216] Award [bit operation]
Dynamic display of analog clock using digital clock in turtle Library
LeetCode 5289. 公平分发饼干(DFS)
Collection of articles on virtualization and cloud computing
架构师必备:系统容量现状checklist
单例模式的实现
C # introductory series (XIII) -- getting to know the structure for the first time
Sort() sort function
I set up a blog
LeetCode 6096. 咒语和药水的成功对数(二分查找)
【20220526】UE5.0.2 release d11782b
攻防世界PWN play 条件竞争漏洞的利用
Jenkins接入Openldap用戶認證
Acwing785. quick sort (sort+ quick sort + merge sort)
LeetCode 6095. 强密码检验器 II
MOOC week 8 programming exercise 1
LeetCode 6098. 统计得分小于 K 的子数组数目(前缀和+二分查找)