当前位置:网站首页>Time complexity
Time complexity
2022-06-23 01:11:00 【Constant constant baa】
Time complexity
1. Constant time operation
If an operation has nothing to do with the data volume of the sample , Every time is a fixed time to complete the operation , This operation is called a constant operation .
Example :int a=arr[i]; a take i The number of positions is taken out , Is a fixed time , And arr It doesn't matter the amount of data , This is a constant operation .
conversely ,int b =list.get[i]; It's a linked list ,b Values need to be taken one by one from the beginning of the linked list ,i In the thousands , Just check how many people , It's about the amount of data , Not a constant operation .
Add, subtract, multiply and divide , Bit operations are all constant operations .
2. Time complexity
Time complexity is an algorithm flow , An indicator of the number of constant operations . Commonly used o Express , pronounce as big o.
say concretely , How many constant operations are performed in an algorithm flow ( use N Express ), Sum up the quantitative expression of constant operation (N The expression of ), Only the highest order terms are left , And not the coefficient of the highest order term , If the rest is f(N), So the time complexity is o(f(N)).
3. Evaluate the algorithm flow
Evaluate the flow of an algorithm , Let's look at the time complexity index first , Highest order items . If the same , Then analyze the actual running time under different data samples , That is to say “ Constant term time ”.( Just look at N The coefficient of the expression , If you N1 yes 10,N2 yes 100, however N2 Of 100 There are many bit operations in ,N1 Of 10 Middle is addition, subtraction, multiplication and division , Can't directly because 10<100 Just say N1 Better algorithm , Because the constant operation time of addition, subtraction, multiplication and division is different from that of bit operation , Not sure , We need to experiment with the actual situation .)
Encounter the same indicators , Directly by experiment , Run the code separately to judge .
Example :
` public static void text1(){
int N =10000;
int a=0;
for(int i=0;i<N;i++){
a=12+100;
a=32*12;`
}
}
`public static void text2(){``
int N =10000;
int a=0;
for(int i=0;i<N;i++){
a=12|100;
a=746^12;
}
}
text1 and text2 The indicators of time complexity are the same N, Next, direct code tests to determine which is better , There is no need to go to the ratio coefficient .
边栏推荐
- Read Amazon memorydb database based on redis
- Because I said: volatile is a lightweight synchronized, the interviewer asked me to go back and wait for the notice!
- MySQL - SQL execution process
- Add expiration time for localstorage
- Have you stepped on these pits? Use caution when creating indexes on time type columns
- BGP federal comprehensive experiment
- How to set the power-off auto start of easycvr hardware box
- Prevent others from using the browser to debug
- 打新债到底靠不靠谱呀?是不是安全的?
- 07 project cost management
猜你喜欢
![[sliding window] leetcode992 Subarrays with K Different Integers](/img/69/1ac0c54d33af0f7a9e3db3e82d076b.png)
[sliding window] leetcode992 Subarrays with K Different Integers

崔鹏团队:万字长文梳理「稳定学习」全景图

Sélecteur de hiérarchie

How to refine permissions to buttons?

数据库中数据的储存结构和方式是什么?

New progress in the construction of meituan's Flink based real-time data warehouse platform

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

How to calculate the position of gold ETF

OSPF comprehensive experiment

MySQL - SQL execution process
随机推荐
【滑动窗口】leetcode992. Subarrays with K Different Integers
Extend your kubernetes API using the aggregation API
Dual cross domain: access allow origin header contains multiple values "*, *", but only one is allowed
EasyCVR使用RTMP推流时不显示界面如何解决?
How to get started with machine learning?
[initial launch] there are too many requests at once, and the database is in danger
cadence SPB17.4 - 中文UI设置
MGRE环境下的OSPF实验
SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
打新债属于什么理财产品?
SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
Read Amazon memorydb database based on redis
Add / get and delete cookies
Learn the specific technical learning experience of designing reusable software from the three levels of class, API and framework
总体标准差和样本标准差
E-R diagram
Swiftui swift tutorial 14 useful array operators
a++,++a,!,~
leetcode 91. Decode Ways 解码方法(中等)
How to calculate the position of gold ETF