time complexity analysis of an algorithm
(1) Time frequency
The time it takes for an algorithm to execute is theoretically impossible to figure out and must be tested on the machine. But we can not and do not need to test each algorithm, just know which algorithm spends more time, which algorithm spends less time on it. And the time that an algorithm spends is proportional to the number of executions of the statement in the algorithm, which algorithm takes more time than the number of statements executed. The number of times a statement is executed in an algorithm is called a statement frequency or time frequency. Note as T (N).
(2) Complexity of time
In the time frequency mentioned just now, N is called the scale of the problem, and when N is constantly changing, the time frequency t (n) will change constantly. But sometimes we want to know what the pattern is when it changes. To do this, we introduce the concept of time complexity.
under normal circumstances, the number of iterations of the basic operation of the algorithm is a function of the problem size n, denoted by T (n), if there is an auxiliary function f (n), so that when n approaches infinity, the limit value of T (n)/f (n) is not equal to zero constant, then f (n) is the same order of magnitude function of t As T (n) =o (f (n)), called O (f (n)) is the progressive time complexity of the algorithm, which is referred to as the complexity of time. O is an order of magnitude symbol.
In order of magnitude increments, common time complexity is:
C < log2n < n < n * log2n < n^2 < N^3 < 2^n < 3^n < n!
Two computational time complexity
1, non-recursive function to calculate the complexity of time
2. Recursive function to calculate the complexity of time
Calculation of time complexity by non-recursive function
(1) Identify key operations
(2) Calculate the execution steps of the key operation
(3) Computational Complexity O ()
Recursive functions for calculating time complexity
(1) Calculating the structure of recursion
(2) List of recursive equations
(3) The recursive equation (a relatively simple algorithm is the iterative calculation)
Time complexity and spatial complexity of commonly used algorithms:
Average time o (n2 ) o (n2 ) o (n2 ) o (n2 ) o (logr b) o (NLOGN) o (n2 ) o (NLOGN) o (NLOGN) stable
Sort by | TD style= "Font-size:1em" >
worst case |
stability |
additional space |
remarks |
bubbling | TD style= "Font-size:1em" >
o (n2 ) |
stable |
o (1) |
n less good |
interchange | TD style= "Font-size:1em" >
o (n2 ) |
unstable |
o (1) |
n less good |
select | TD style= "Font-size:1em" >
o (n2 ) |
unstable |
o (1) |
n less good |
insert | TD style= "Font-size:1em" >
o (n2 ) |
stable |
o (1) |
Most of them are sorted better |
cardinality | TD style= "Font-size:1em" >
o (LogR B) |
stable |
o (n) |
b is true number (0-9), < Span style= "font-size:14px" >r is cardinality (1000) |
shell |
o (NLOGN) |
o (ns ) 1<s< 2 |
unstable |
o (1) |
s is the selected grouping |
quick | TD style= "Font-size:1em" > TD style= "Font-size:1em" >
unstable |
o (NLOGN) |
n better |
merge | TD style= "Font-size:1em" > TD style= "Font-size:1em" > TD style= "Font-size:1em" >
o (1) |
n better |
Heap |
O (NLOGN) |
O (NLOGN) |
Not stable |
O (1) |
Better when n is larger
|
Three dimensions of space complexity
The spatial complexity of a program is the amount of memory required to run a program. With the spatial complexity of the program, you can have a pre-estimate of how much memory is needed to run the program. In addition to requiring storage space and storing the instructions, constants, variables, and input data used by the store itself, a program needs some working units to manipulate the data and a secondary space to store some of the information needed for realistic computing. The storage space required for program execution consists of the following two parts.
(1) fixed section
(2) variable space
The storage space required for an algorithm is represented by F (n). s (n) =o (f (n)) where n is the size of the problem, S (n) represents spatial complexity.
Analysis of time complexity and spatial complexity of the algorithm