当前位置:网站首页>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 .
边栏推荐
猜你喜欢
JUC concurrent knowledge points
【Leetcode刷题】数组1——双指针
官方教程 Redshift 03 各种GI的参数和常规使用说明
Maya aces workflow configuration (Arnold and redshift map configuration specification - restore the correct effect of the map under the SP aces process) PS restore the rendered map under the aces proc
模型空间下的旋转和世界空间下的旋转
官方教程 Redshift 02 4中GI引擎概述及总结
Dynamic planning summary
官方教程 Redshift 06 Opt参数
Overview and summary of GI engine in redshift 024, the official tutorial
STP生成树原理及选举规则举例
随机推荐
Redshift 2.6.41 for maya2018 水印去除
Leetcode 9. palindromes
LeetCode #13. 罗马数字转整数
Leetcode 3. longest substring without repeated characters
[beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
Oracle10g出现Enterprise Manager 无法连接到数据库实例解决办法
官方教程 Redshift 05 system参数详细解释
官方教程 Redshift 08 Light
c语言面试准备一(谈谈理解系类)
MySQL interview questions
UE4/UE5 C盘变大处理
Leetcode 7. integer inversion
Multithreading and concurrency
Unity初学1——角色移动控制(2d)
Single chain surface test questions
Eight sorts ----------- bubble sort
只让电脑运行某个程序设置
[beauty of software engineering - column notes] 14 | project management tools: all management problems should be considered whether they can be solved by tools
模型空间下的旋转和世界空间下的旋转
Joiner.on和stream().map联合使用技巧