The idea of several sorting algorithms is easy to grasp, is the corresponding time complexity, the reason is the time complexity is what, how to define the calculation is not known, then the time complexity is how to calculate it. Please see below.
Before talking about the time complexity to say the algorithm, the algorithm is to solve a problem and take the specific , limited Operating Procedures , since the algorithm is the operation of the steps, then the steps to occupy the computer resources of how much determines the efficiency of the algorithm. While computer resources have time resources (processors) and space Resources (memory), time complexity is one of the criteria to describe the efficiency of the algorithm.
What is the complexity of time?
In the algorithm, the number of times the operation repeats is the time measurement of the algorithm.
F (n) is a function that describes the number of times an algorithm repeats execution, and the time complexity of the algorithm is O (f (n)). Here only the demand for the function (f (n)) of the growth rate.
Example 1:
F (n) =n^3+3n O (f (n)) =n^3
F (n) =2 O (f (n)) =1
F (n) =logn+n O (f (n)) =logn
How to solve
Now, given the function of one of our algorithms, we can certainly easily write out the complexity of time, but the function is often not directly to us, to us is a piece of code, we are easy to find the time complexity of it.
As follows:
Example 2.1:
for (I=1;i<=n;++i) {for
(j=1;j<=n;++j) {
++x;
s+=x;
}
}
Analysis: 1) The number of repetitions of the first for loop is n
2) The second for repeat number is also n
3) Two for is nested, the total number of repetitions of the program is n^2
4) The complexity of time is n^2
Example 2.2:
I=1;
while (i<=n) {
i=i*2;
}
Analysis: 1) Set the number of repetitions to M
2) The value of the I=1 cycle I is multiplied by 2, the value of I after the M-cycle is not satisfied with less than or equal to N, the function i* (2^m) >n (I=1)
3) The number of repetitions m=log2 (n/i), which is the time complexity
Note: In the title I if not 1 o'clock, the time complexity or log2 (n/i), the reason is to seek the growth rate of the function.
Well, now that we know how to find the time complexity of the function and time complexity, a single function can be found, then a number of functions in a piece. It's like we're adding, subtracting, and multiplying, so how does a mixed operation count? Complex program can write multiple time complexity of the function, the whole function of the time complexity calculation is like the priority of the mixed operation, there are rules, please continue to see:
The representation of time complexity addition rule: for the parallel program O (f (N1)) +o (f (N2)) =o (Max (f (N1), F (N2)))
multiplication rule: for nested programs O (f (N1)) *o (f (N2)) =o (f (N1) *f (n2))
Exception: If one of them is a constant c
F (N1) =c O (f (N1)) *o (f (N2)) =o (C*f (n2)) = O (f (n2))
Example 3:
{for
(int i = 0; i < m; i++) {
sum[i] = 0;
for (int j = 0; J < N; j + +) {
Sum[i] + = X[i][j];}
}
for (i = 0; i < m; i++) {
System.out.println (i);
}
}
Analysis: 1) nested two for loops, using the multiplication rule: O (m*n)
2) A third for loop with a time complexity of O (m)
3) Using the addition rule: the time complexity is O (Max (O (m*n), O (m)))