当前位置:网站首页>01-复杂度
01-复杂度
2022-06-12 18:02:00 【咸鱼~翻身】
1.什么是算法
- 算法是用于解决特定问题的一系列的执行步骤
2.要和评判一个算法的好坏
- 正确性、可读性、健壮性
- 时间复杂度:估算程序指令的执行次数
- 空间复杂度:估算所需占用的存储空间
3.大O表示法
- 大O表示法仅仅是一种粗略的分析模型
常见的复杂度
| 执行次数 | 复杂度 | 非正式术语 |
|---|---|---|
| 12 | O(1) | 常数阶 |
| 2n+3 | O(n) | 线性阶 |
| 4n^2+2n+6 | O(n2) | 平方阶 |
| 4log2 n+25 | O(logn) | 对数阶 |
| 3n+2nlog3 n+15 | O(nlogn) | nlogn阶 |
| 4n^3+3n^2+22n+100 | O(n3) | 立方阶 |
| 2^n | O(2n) | 指数阶 |
- O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n3)<O(2n)<O(n!)<O(nn)
说明:
- 对数阶一般省略底数
- 因为所有的对数都可以转换为log2 n = log2 9 * log9 n
4.时间复杂度
- 时间复杂度:估算程序指令的执行次数
时间复杂度为O(n)
int A(int x, int n) {
int sum = 1; // 注意 任何数的0次方等于1
for (int i = 0; i < n; i++) {
sum = sum * x;
}
return sum;
}
时间复杂度为O(n)
int A(int x, int n) {
if (n == 0) {
return 1; // return 1 同样是因为0次方是等于1的
}
return A(x, n - 1) * x;
}
- 递归算法的时间复杂度本质上是要看: 递归的次数 * 每次递归中的操作次数。
时间复杂度为O((logn)
int sum = 1;
while(sum < n){
sum = sum * 2;
}
5.空间复杂度
- 空间复杂度:估算所需占用的存储空间
空间复杂度为O(1)
int j = 0;
for (int i = 0; i < n; i++) {
j++;
}
空间复杂度为O(n)
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i]=i;
}
边栏推荐
- JDBC several pits
- Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled
- Kali2022 installing Armitage
- Kali2022安装armitage
- Message queuing MySQL tables that store message data
- 机器学习系列(5):朴素贝叶斯
- Gossip about the 88 of redis source code
- Vant3+ts encapsulates uploader upload image component
- Authorization in Golang ProjectUseing Casbin
- High-Speed Layout Guidelines 未完...
猜你喜欢

Vant3+ts encapsulates uploader upload image component

USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate

机器学习系列(5):朴素贝叶斯

DRM driven MMAP detailed explanation: (I) preliminary knowledge

MySQL学习笔记
![Vulnhub[DC3]](/img/3a/1aa03e804d447d38e85807928fdb8f.png)
Vulnhub[DC3]

Applet and app are owned at the same time? A technical scheme with both

轻量、便捷的小程序转App技术方案,实现与微信/流量App互联互通

USB转串口那些事儿—最大峰值串口波特率VS连续通信最高波特率

First principles of enterprise architecture
随机推荐
Applet and app are owned at the same time? A technical scheme with both
Esp32-c3 esp-idf configuring smartconfig and SNTP to obtain network time
[csp]202012-2 optimal threshold for period end forecast
一种好用、易上手的小程序IDE
Project training of Software College of Shandong University - Innovation Training - network attack and defense shooting range experimental platform of Software College of Shandong University (XXV) - p
Introduction to reinforcement learning and analysis of classic items 1.3
Article name
Second week of electric control learning
vant3+ts 封装uploader上传图片组件
High speed layout guidelines incomplete
About datasets
Reconstruction -- sort out and decompose the inheritance system
ES7 does not use parent-child and nested relationships to implement one to many functions
Database SQL operation Basics
Use the official go Library of mongodb to operate the original mongodb
轻量、便捷的小程序转App技术方案,实现与微信/流量App互联互通
73. 矩阵置零(标记法)
Vulnhub[DC3]
vant3+ts+pinia tab选项卡列表页面点击进详情,详情页返回tab高亮在原位置,刷新高亮默认在第一项
App中快速复用微信登录授权的一种方法