Efficiency of the algorithm from the following two aspects:
- Time Efficiency: it refers to the time it takes for the algorithm
- Space Efficiency: refers to the process spent executing the algorithm in the storage space
Time efficiency and space efficiency are sometimes contradictory.
Here we only discuss pre-analysis, post hoc analysis method also because the objective conditions and other computer hardware and software and so on.
Prior analysis
run time of the algorithm is substantially equal to a computer to perform a simple operation (such as assignment, comparing, moving, etc.) and the time required to perform the algorithm is simple product number .
Matrix, for example:
for(i=1;i<=n;i++) //n+1次
for(j=1;j<=n;j++) //n*(n+1)次
{
c[i][j]=0; //n*n次
for(k=0;k<=n;k++) //n*n*(n+1)次
c[i][j]=c[i][j]+a[i][k]*b[k][j];//n*n*n次
}
Then the time consumed by the above algorithm is that the algorithm execution times each statement sum, the time consumed T (n-) = 2N . 3 + with 3N 2 + 2N +. 1.
In order to facilitate comparison of different time efficiency of the algorithm, we only compare their magnitude.
- If an auxiliary function f (n) (i.e., containing only a function of the highest magnitude), such that when n approaches infinity, T (n) / f ( n) is the limit value not equal to zero constants , called f ( n) is T (n) is a function of the same order. Denoted by T (n-) = O (f (n)) , said O (f (n)) is the time complexity.
Analysis algorithm time complexity:
- Find out the statement is the maximum frequency that statement (that is, the maximum number of execution algorithm) as the basic statement
- Calculation of basic statement of the frequency of the resulting problem size n of a function f (n)
- Whichever is highest magnitude represented by symbol O
Time complexity is determined by the frequency of nested deepest statement.
example:
void exam(float x[][],int m,int n)
{
float sum[];
for(int i=0;i<m;i++)
{
sum[i]=0.0;
for(int j=0;j<n;j++)
sum[i]+=x[i][j];//嵌套最深层
}
example:
i=1;
while(i<=n)
i=i*2;
Set the number of executions of X
2 X <= n-
X <= log 2 n-
execute the maximum number of log 2 n-, T (n-) = O (log 2 n-)