https://blog.csdn.net/zxm490484080/article/details/72210501
The complexity of the algorithm is divided into time complexity and space complexity.
Its role:
Time complexity refers to the computational effort required to perform the algorithm;
Spatial complexity refers to the amount of memory space required to execute the algorithm.
(The complexity of the algorithm is reflected in the amount of resources required to run the algorithm, the most important thing in computer resources is time and space (that is, register) resources, so complexity is divided into time and space complexity).
In short, time complexity refers to the number of execution times, the space complexity refers to the storage space occupied by the algorithm
Complexity of Time
Method of calculating time complexity: substituting constant 1 for all addition constants in the runtime modified run-times function, only the coefficients of highest order are preserved
In order of magnitude increments, common time complexity is:
Constant order O (1), Logarithmic order O (log2n), linear order O (n),
Linear logarithmic order O (nlog2n), square order O (n^2), Cubic O (n^3),...,
K-Order O (n^k), index order O (2^n).
With the increasing of the problem scale N, the complexity of the time is increasing and the efficiency of the algorithm is less.
Give me a chestnut:
sum = N (n+1)/2; Time complexity O (1)
1
for (int i = 0; i < n; i++) {
printf ("%d", I);
}
Time complexity O (n)
1 2 3 4
for (int i = 0; i < n; i++) {for
(int j = 0; J < N; j + +) {
printf ("%d", I);}
}
Time complexity O (n^2)
1 2 3 4 5 6
for (int i = 0; i < n; i++) {for
(int j = i; J < N; j + +) {
printf ("%d", I);}
}
Number of runs (1+n) *N/2
//Time complexity O (n^2)
1 2 3 4 5 6 7
int i = 1, n = +;
while (I < n) {
i = i * 2;
}
Set the number of executions to X. 2^x = n = x = log2n
//Time complexity O (log2n)
1 2 3 4 5 6
Worst time complexity and average time complexity
The worst-case time complexity is called the worst time complexity. In general, it is not particularly stated that the time complexity of the discussion is the worst-case time complexity.
The reason for this is that the worst-case time complexity is the upper bound of the algorithm's run time on any input instance, which guarantees that the algorithm will not run longer than any other.
The average time complexity is the expected run time of the algorithm when all possible input instances are present with equal probabilities. The probability of the occurrence of each case is pi, and the average time complexity is sum (PI*F (n))
Time complexity of common sorting algorithms
Worst time analysis average time complexity stability spatial complexity
bubble sort O (n2) O (n2) stable O (1)
quick sort O (n2) O (n*log2n ) Unstable O (log2n) ~o (n)
select sort O ( n2) O (n2) stable O ( 1)
two fork tree sort O (n2) O (n*log2n Unstable O ( N)
insertion sort O ( n2) O ( n2) stabilized O ( 1)
heap sequencing O (n*log2n) O (n*log2n) unstable O (1)
Hill sort o o unstable O (1)
1 2 3 4 5 6 7 8
Complexity of space
Spatial complexity (space complexity) is a measure of how much storage space is temporarily occupied by an algorithm while it is running, and is recorded as S (n) =o (f (n)).
For an algorithm, spatial complexity and time complexity are often influenced by each other. When the pursuit of a better time complexity, the performance of the spatial complexity may be poor, that is, may lead to more storage space; Conversely, when the pursuit of a better spatial complexity, the performance of time complexity may be poor, which may lead to a longer run time.
Sometimes we can use space in exchange for time to achieve our goal.