当前位置:网站首页>Personal views on time complexity
Personal views on time complexity
2022-07-29 06:27:00 【C2024XSC249】
Concept
In computer science , time complexity , also called Time complexity , Algorithm Time complexity It's a function , It qualitatively describes the running time of the algorithm . This is a function of the length of the string representing the input value of the algorithm . Time complexity is often large O Symbolic representation , The lower order and first term coefficients of this function are not included . When using this method , Time complexity can be called asymptotic , That is, the size of the input value is approaching infinity .

Constant order
for example :
#include <cstdio>
using namespace std;
int main () {
int a, b;
scanf ("%d %d", &a, &b);
int c = a + b;
printf ("%d", c);
return 0;
}
This code , No matter what you enter a and b What is the value of , Will only run once , So write it down as Constant order : O ( 1 ) O (1) O(1)
Linear order
for example :
#include <cstdio>
using namespace std;
int main () {
int n, sum = 0;
scanf ("%d", &n);
for (int i = 1; i <= n; i ++) {
sum += n;
}
printf ("%d", sum);
return 0;
}
This code , The number of runs is determined by n decision , So write it down as Linear order : O ( n ) O (n) O(n)
Square order
for example :
#include <cstdio>
using namespace std;
int main () {
int n, sum = 0;
scanf ("%d", &n);
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
sum += i * j;
}
}
printf ("%d", sum);
return 0;
}
This code , The number of runs is determined by n decision , But there are two nested loops , So write it down as Square order : O ( n 2 ) O (n^{2}) O(n2)
Similarly , If nested k Secondary cycle , Write it down as k Order of second order : O ( n k ) O (n^{k}) O(nk)
Logarithmic order
for example :
#include <cstdio>
using namespace std;
int main () {
int n, sum = 0;
scanf ("%d", &n);
while (n != 0) {
n /= 2, sum ++;
}
printf ("%d", sum);
return 0;
}
This code , The number of runs is calculated n How many of them 2, So write it down as Logarithmic order : O ( log n ) O (\log n) O(logn)
Similarly , If we combine linear order and logarithmic order , Write it down as Linear logarithmic order : O ( n log n ) O (n \log n) O(nlogn)
Exponential order
for example :
#include <cstdio>
using namespace std;
long long digui (int n) {
if (n == 1 || n == 2) {
return 1;
}
return digui (n - 1) + digui (n - 2);
}
int main () {
int n, sum = 0;
scanf ("%d", &n);
for (int i = 1; i <= n; i ++) {
sum += n;
}
printf ("%d", sum);
return 0;
}
This code , Fibonacci series , Need to compute 2 n 2^{n} 2n Time ( No explanation here ), Write it down as Exponential order : O ( 2 n ) O (2^{n}) O(2n)
summary
About the analysis of time complexity, the author is here , If there are mistakes or omissions , Please bosses Don't hesitate to correct .
边栏推荐
- Eight sorts ------------- heap sort
- [beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
- IDEA安装scala
- 关于【链式前向星】的自学理解
- 进程与线程
- 官方教程 Redshift 05 AOVs
- Leetcode notes 605. can place flowers (easy) 605. planting flowers
- Leetcode 1. sum of two numbers
- 官方教程 Redshift 05 system参数详细解释
- 9196 tumor area solution
猜你喜欢

Ue5 texture system explanation and common problem setting and Solutions

Leetcode 35. search insertion location
![[leetcode brush questions] array 3 - divide and conquer](/img/76/bc3d9ba0b84578e17bf30195bda5d1.png)
[leetcode brush questions] array 3 - divide and conquer

官方教程 Redshift 04 渲染参数

JVM内存结构

LeetCode #167.两数之和 II - 输入有序数组

Dynamic planning summary

LeetCode #13. 罗马数字转整数

Leetcode 283. move zero

UE4/UE5 C盘变大处理
随机推荐
Leetcode 14. longest public prefix
Oracle10g出现Enterprise Manager 无法连接到数据库实例解决办法
LeetCode #83. 删除排序链表中的重复元素
9196 tumor area solution
模型空间下的旋转和世界空间下的旋转
滑动窗口 Leetcode 76.最小覆盖子串(困难) 76.76. MinimumWindow Substring (Hard)
Eight sorts --------- quick sort
进程与线程
[beauty of software engineering - column notes] 16 | how to write project documents?
官方教程 Redshift 04 渲染参数
虹科分享 | 带你全面认识“CAN总线错误”(二)——CAN错误类型
STP生成树原理及选举规则举例
【Leetcode刷题】数组1——双指针
LeetCode #13. 罗马数字转整数
Joiner.on和stream().map联合使用技巧
Leetcode - Tips
Leetcode 189. rotation array
Eight sorts ------------- heap sort
官方教程 Redshift 08 Light
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置