content
1. The concept of time complexity
2. Asymptotic notation for Big O
Computational space complexity
Example 4: Calculate the time complexity of bubble sort
Example 5: Time complexity of binary search
Example 6: Calculate the time complexity of factorial recursion
Example 7: Calculate the time complexity of Fibonacci recursion
Example 1: Calculate the space complexity of bubble sort
Example 2: Calculate the space complexity of Fibonacci
Example 3: Calculate the space complexity of factorial recursion
Foreword :
The so-called complexity is to measure the efficiency of the algorithm, and the calculation efficiency is divided into two types, one is called time complexity and the other is called space complexity.
1. Algorithm Efficiency
There are two types of algorithm efficiency analysis: the first is time efficiency, and the second is space efficiency. Time efficiency is called time complexity, while space efficiency is called space complexity. Time complexity mainly measures the running speed of an algorithm, while space complexity mainly measures the extra space required by an algorithm. In the early days of computer development, the storage capacity of computers was very small. So it is very concerned about the space complexity. But after the rapid development of the computer industry, the storage capacity of the computer has reached a very high level. So we no longer need to pay special attention to the space complexity of an algorithm.
2. Time complexity
1. The concept 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 to say, when we get a code and look at the time complexity of the code, we mainly find out how many times the code that executes the most statements in the code is executed.
2. Asymptotic notation for Big O
Look at the picture analysis:
When the value of N becomes larger and larger, the values of 2N and 10 can be ignored.
In practice, when we calculate the time complexity, we do not necessarily need to calculate the exact number of executions, but only the approximate number of executions, so here we use the asymptotic representation of big O.
Big O notation: is a mathematical notation used to describe the asymptotic behavior of a function.
1. Replace all additive constants in runtime with the constant 1.
2. In the modified run times function, only the highest-order term is retained.
3. If the highest-order term exists and is not 1, remove the constant multiplied by this term. The result is a big-O order.
Through the above, we will find that the asymptotic representation of big O removes those items that have little effect on the result, and shows the number of executions succinctly and clearly.
In addition, the time complexity of some algorithms has a best, average and worst case:
Worst case: maximum number of runs for any input size (upper bound)
Average case: desired number of runs for any input size
Best case: minimum number of runs for any input size (lower bound)
For example: search for a data x in an array of length N
Best case: 1 find
Worst case: N times found
Average case: N/2 found
In practice, the general concern is the worst-case operation of the algorithm, so the time complexity of searching for data in the array is O(N)
Computational space complexity
Example 1:
The basic operation is performed 2N+10 times. By deriving the big O-order method, we know that the time complexity is O(N)
Example 2:
The basic operation is performed M+N times, there are two unknowns M and N, and the time complexity is O(N+M)
Example 3:
The basic operation is performed 100 times, and the time complexity is O(1) by deriving the big-O-order method
Example 4: Calculate the time complexity of bubble sort
The basic operation is best performed N times, and the worst is performed (N*(N-1))/2 times. By deriving the big O-order method + the time complexity is generally the worst, and the time complexity is O(N^2
Example 5: Time complexity of binary search
The basic operation is best performed once, the worst is O(logN) times, and the time complexity is O(logN) ps: logN means that the base is 2 and the logarithm is N in the algorithm analysis. In some places it will be written as lgN. (It is recommended to explain how logN is calculated by means of origami search) (because the binary search eliminates half of the unsuitable values each time, once the two points are left: n/2 twice the two points are left: n/2/2 = n /4)
Example 6: Calculate the time complexity of factorial recursion
The time complexity of recursion = the number of recursion * the number of times each recursion is executed
Through computational analysis, it is found that the basic operations recurse N times, and the time complexity is O(N).
Example 7: Calculate the time complexity of Fibonacci recursion
Through computational analysis, it is found that the basic operation recurs 2^N times, and the time complexity is O(2^N).
law:
2^0+2^1+2^2+2^3……2^(n-(n-1))
Summation of proportional series
a1 represents the first item, q is equal to 2, 1(1-2^n)/-1, which is equivalent to 2^n+1, so the time complexity is O(2^n)
3. Space complexity
Space complexity is a measure of the amount of storage space temporarily occupied by an algorithm during its execution. The space complexity is not how many bytes the program occupies, because this does not make much sense, so the space complexity is the number of variables. The space complexity calculation rules are basically similar to the practical complexity, and also use the big-O asymptotic notation.
Example 1: Calculate the space complexity of bubble sort
A constant amount of extra space is used, so the space complexity is O(1)
Example 2: Calculate the space complexity of Fibonacci
Dynamically opens up N spaces, and the space complexity is O(N)
Example 3: Calculate the space complexity of factorial recursion
The recursive call is made N times, and N stack frames are opened up, and each stack frame uses a constant amount of space. The space complexity is O(N)
Summary :
This article briefly introduces what time complexity and space complexity are, and deepens the understanding of arrays through simple examples. The above is the content of today. If you have any questions, you can privately message me at any time. I will actively correct any problems in the article. I also hope that everyone can master the knowledge they want faster, let’s work together! ! ! ! !
I am with you . _