Detailed explanation of the time complexity of the algorithm


Table of contents

foreword

The concept of time complexity

 Example one:

Big O asymptotic notation

Common time complexity examples (including analysis)

Example 1:

Example three:

Example four:

Example five:

Example six:

Example 7:

Example eight:


foreword

Avoid inefficiency and consume more resources when dealing with large-scale problems, so the algorithm complexity is introduced, which can measure the efficiency and feasibility of the algorithm and help to choose the optimal algorithm to solve the problem;

The concept of time complexity

In computer science, the time complexity of an algorithm is a function that quantitatively describes the running time of that algorithm. The time it takes to execute an algorithm cannot be calculated theoretically. You can only know it when you put your program on the machine and run it
. But do we need to test each algorithm on the computer? It is possible to test on the computer, but it is very troublesome, and each person's machine is different, the influence of factors such as the network, the time measured at different times and places is different, so there is the analysis method of time complexity. The time spent by an algorithm is proportional to the number of executions of the statements in it, and the number of executions of the basic operations in the algorithm is the time complexity of the algorithm.
That is: finding the mathematical expression between a certain basic sentence and the problem size N is to calculate the time complexity of the algorithm.

As shown in Example 1 below

 Example one:

analyze:

we can easily know

There is also a for loop nested in the first for loop, which will be executed N*N times in total;

The second for loop will execute 2*N times;

The while loop will execute 10 times;

So the number of basic operations performed by     Func1 : N^2 +2*N +10

However, it should be noted that in practice, when calculating the time complexity, it is not necessary to calculate the exact number of executions, but only to calculate the approximate number of times and grasp the big head. This representation method is called big O asymptotic representation ( The rules of this method are explained in detail below)

For example N^2+2*N+10  

当 N = 10 F(N) = 130
        N = 100 F(N) = 10210
        N = 1000 F(N) =

As N gets bigger and bigger, the value of 2*N +10 is compared with the value of N^2, and the value of 2*N +10 is too small to be ignored. Here, the time complexity is represented by big O asymptotically Recorded as O(N^2)

Big O asymptotic notation

Big O notation (Big O notation): is a mathematical notation used to describe the asymptotic behavior of a function.
Derivation of the big O order method:
1. Replace all additive constants in the running time with a constant 1;
2. In the modified running times function, only the highest order term is kept; (for example, N^2 + 2*N +10 above The highest order item of N in N is N^2)
3. If the highest order item exists and is not 1, remove the constant multiplied by this item. The result obtained is the big O order;

(For example, the highest order of 2*N^2 + N + 40 exists, which is 2*N^2, which is expressed as N^2 in big O asymptotic notation, and the coefficient needs to be removed)


Through the above, we will find that the progressive representation of big O removes those items that have little influence on the result, and expresses the number of executions concisely and clearly.
4. In addition, the time complexity of some algorithms has the best, average and worst cases: (time complexity takes the worst case)
 worst case : the maximum number of runs (upper bound) of any input scale
 average case : any input scale
 Best-case expected number of runs : Minimum number of runs for any input size (lower bound)


For example : Search for a data x in an array of length N (example code below Example 4)
 Best case: 1 time to find
 Worst case: N times to find
 Average case: N/2 times to find
In practice, the general situation is concerned with The worst running case of the algorithm, so the time complexity of searching data in the array is O(N)

Common time complexity examples (including analysis)

Example 1:

 analyze:

The first for loop loops 2*N times

The while loop loops 10 times

So the total number of times is 2*N -10

The time complexity of Func2 according to the above rules 2 and 3 is: O(N);

Example 2:

 analyze:

The first for loop loops M times;

The second for loop loops N times;

The total number of executions is M+N times

Due to the relationship between M and N (the time complexity of the size relationship c3 is O(M+N)

If the question says M>>N, then the time complexity of Func3 is O(M)

                  N>>M, the time complexity of Func3 is O(N)

If M=N then the total number of executions is 2*M or then 2*M so the time complexity is O(M) or then O(N) (by rule 3 above)

Example three:

 analyze:

The algorithm has only one for loop, executed k=100 times;

According to the above rule 1, the time complexity of Func4 is: O(1)

Example four:

analyze:

The role of the strchr function is to find the position where the target character first appears in the string in a string, and return the position;

    Best case: 1 time to find the target character at the first element of the string;
    worst case: N times to find the target character at the end of the string;
   average case: N/2 times to find             

According to rule 4 above, the time complexity of strchr is O(N)  

Example five:

analyze:

It is easy to know from the above code that the algorithm is a bubble sort algorithm;

In the best case, when the traversal is completed, there is no exchange of elements, that is, the exchange is 0, jump out of the loop N times to find

In the worst case, all elements are unordered, then the first time is traversed N times, the second time is traversed N-1 times, the third time is traversed N-3 times, and so on, the total number of times is N+(N-1 )+(N-2)………+(2)=( 2+N)*N/2 times

According to the rules 2 and 4 above, it can be seen that the time complexity of BubbleSort is: O(N^2)

Example six:

 analyze:

As can be seen from the code shown, the algorithm is a binary search method;

Best case: 1 found

Worst case: When there is only one element left in the search, or the element has not been found after the search;

 So the time complexity of binary search is O(log N)

Example 7:

analyze:

When calculating the factorial of N, it needs to recurse N times;

So the time complexity of factorial recursion is O(N)

Example eight:

 analyze:

 From graph analysis, the time complexity is O(2^N)

End of this chapter~ The detailed explanation of space complexity is being updated                                     

                                                                                                                       Author: GOTXX


Guess you like

Origin blog.csdn.net/2301_77509762/article/details/131882504
Recommended