当前位置:网站首页>Deep analysis of time complexity
Deep analysis of time complexity
2022-06-23 02:30:00 【User 4196462】
For the comrades who study algorithms , There is no lack of learning about the complexity of time , The learning of time complexity is recorded here , If there is any mistake , Please comment .
Literally , Is the running time of an algorithm , Many little friends thought of running the algorithm program again , Then the time consumed will be known naturally , This way is possible , But there are many drawbacks , It is easily affected by the operating environment , The performance varies greatly , It also has a bearing on the size of the data used . So I still can't run it completely .
Such a universal way was born ,「 Big O Symbolic representation 」, stay Big O In symbolic representation , The formula of time complexity is : T(n) = O( f(n) ), among f(n) Represents the sum of execution times per line of code , and O Represents a positive proportional relationship , The full name of this formula is : Progressive time complexity of the algorithm . This method is not used to truly represent the execution time of the algorithm , Indicates the trend of the increase in execution time .
1. Constant order O(1)
int i = 1; int j = 2; ++i; j++; int m = i + j;
2. Linear order O(n)
for(i=1; i<=n; ++i)
{
j = i;
j++;
}
for The code in the loop will execute n All over , So it takes time to n Changed by , This kind of code can be used O(n) To represent its time complexity .
3. Logarithmic order O(logN)
int i = 1;
while(i<n)
{
i = i * 2;
}
stay while Inside the loop , Every time i multiply 2, After finishing ,i distance n It's getting closer . Hypothetical cycle x After that ,i Greater than 2 了 , At this point, the cycle exits , in other words 2 Of x The second power equals n, that x = log2^n That is to say, when the cycle log2^n Later , That's the end of the code . So the time complexity of this code is :O(logn)
4. Linear logarithmic order O(nlogN)
for(m=1; m<n; m++)
{
i = 1;
while(i<n)
{
i = i * 2;
}
}
Linear logarithmic order O(nlogN) It's really easy to understand , Time complexity is O(logn) Code loop for N All over , So its time complexity is n * O(logN), That's it. O(nlogN).
边栏推荐
- The practice of traffic and data isolation in vivo Reviews
- This monitoring tool is enough for the operation and maintenance of small and medium-sized enterprises - wgcloud
- Pychart installation instructions
- Use of apicloud AVM framework list component list view and flex layout tutorial
- C language games: sanziqi (simple version) implementation explanation
- Web components series (I) - Overview
- 2021-11-11
- 51. numerical arrangement
- Spread spectrum and frequency hopping
- JS request path console reports an error failed to launch 'xxx' because the scheme does not have a registered handler
猜你喜欢

Spark broadcast variables and accumulators (cases attached)

Anaconda creates a new environment encounter pit

Rebirth -- C language and the story I have to tell (text)

JS rotation chart (Netease cloud rotation chart)

Performance test -- 14 detailed explanation of performance test report and precautions

II Data preprocessing

what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!

Stop automatically after MySQL starts (unable to start)

Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.

Small knowledge points of asset
随机推荐
Performance test -- 14 detailed explanation of performance test report and precautions
1. Mx6u startup mode and equipment
await is only valid in async function
Rebirth -- C language and the story I have to tell (text)
Ansible practice of Nepal graph
5 trends brought to us by customers
Goframe framework (RK boot): fast implementation of CSRF verification
Analysis of ThreadLocal
Campus network AC authentication failed
Common mistakes in C language (sizeof and strlen)
Using mock data in vite projects -vite plugin mock
Evolution history of mobile communication
The difference between script in head and body
C language foundation ----- write a function to find the larger value of two unequal integers
EDI project cases of customers in medical device industry
Three ways to get class
Direct collection - super easy to use domestic color matching website
Hypervisor Necromancy; Recover kernel protector (1)
Application and challenge of ten billion level map data in Kwai security intelligence
How does the easyplayer streaming video player set up tiling?