Overview
?? An algorithm consists of the control structure (order, branch, Loop) and the original operation (refers to the operation of the intrinsic data type). To facilitate the comparison of different algorithms for the same problem, it is common practice to choose from the algorithm a primitive operation that is basic to the problem being studied, with the number of repetitions of the primitive operation as the time measurement of the algorithm. in most cases, the primary operation is the original operation in its deepest loop, and the time measurement of the algorithm is most commonly considered in the worst case of time complexity.
definition of time complexity
?? 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 (O is the order of magnitude), referred to as the time complexity.
according to the definition, the basic calculation steps can be summed up
1. Calculate the number of executions of the basic operation T (n)
?? The basic operation is that each statement in the algorithm (by; number as a partition), the number of executions of the statement is also called the frequency of the statement. When doing algorithmic analysis, the general default is to consider the worst case scenario.
2. Calculate the order of magnitude of T (N)
?? The order of magnitude of T (N), as long as T (n) is omitted, the coefficients of the constant, the lower power, and the highest power are ignored. The order of magnitude of F (n) =t (n).
3. Use large o to represent the complexity of time
?? When n approaches infinity, if the value of Lim (T (n)/f (n) is a constant that is not equal to 0, then f (n) is said to be the same order of magnitude function of T (N). Note as T (n) =o (f (n)).
The above steps can simplify
1. Find the most frequently executed statements
2. Count the number of execution times of a statement
3. Use large o to indicate results
Example
Example One (O (n))
publicvoidprintsum(int count){ int1; for(int0; i<n; i++){ sum += i; } System.out.print(sum);}
Remember that only operational statements increase the complexity of time, so the contents of the above method are O (1) In addition to the loop and the rest of the operational statements.
So printsum time complexity = for O (n) +o (1) = Ignore constant = O (n)
Here you can actually apply the formula num = n (n+1)/2, optimization of the algorithm, changed to *
publicvoidprintsum(int count){ int0; sum = count*(count+1)/2; System.out.print(sum);}
The time complexity of the algorithm will be reduced from the original O (n) to O (1), greatly improving the performance of the algorithm.
Example II (O (LOG2N))
int1;while(i<n){ i = i*2;}
Set (i=i*2) frequency is T, then: 2t (2 of the T-square) <=n; Both sides go to logarithmic t<=log2n, consider worst case, take maximum value t=log2n. T (n) = O (log2n).
Example Three (O (N2))
int num=0;for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ num++; }}
The time complexity is O (N2).
time complexity of common algorithms
Reference
http://univasity.iteye.com/blog/1164707
Http://www.cnblogs.com/songQQ/archive/2009/10/20/1587122.html
Calculation of algorithm time complexity