Lesson 2-Soul of the trial procedure
- Algorithm efficiency measurement
L post-event statistics
Compare the running time of different algorithms for the same group of input data.
Defects:
(1) to obtain the running time of different algorithms, you must write the corresponding program.
(2) the running time is heavily dependent on hardware and environmental factors during running.
(3) it is quite difficult to select algorithm test data.
Although the post-event statistics method is intuitive, it is difficult to implement and has many defects, but it is difficult to implement and has many defects, which is generally not considered.
L Advance ANALYSIS AND ESTIMATION
The algorithm efficiency is estimated based on the statistical method.
L main factors affecting algorithm efficiency
(1) policies and methods used by the algorithm.
(2) Input scale of the problem.
(3) code generated by the compiler.
(4) Computer execution speed.
Simple Estimation of algorithm efficiency
- Estimation of algorithm efficiency
Example: Dual-cycle Estimation
# Include <stdio. h>
Int func (int A [], int Len)
{
Int I = 0;
Int J = 0;
Int S = 0;
For (I = 0; I <Len; I ++)
{
For (j = 0; j <Len; j ++)
{
S + = I * J;
}
}
Return S;
}
Int main ()
{
Int array [] = {1, 2, 3, 4, 5 };
Printf ("% d \ n", func (array, 5 ));
Return 0;
}
Estimated time: t = (n2 + 2) T
Inspiration
(1) The number of operations on the key part of the program in the exercise is N * n.
(2) The number of operations on the key parts of the three Summation Algorithms is 2n, N, and 1, respectively.
As the problem scale N increases, the number of their operations will also increase, so the actual algorithm will become very obvious in time efficiency!
Comparison of Different algorithm operations
When determining the efficiency of an algorithm, you often only need to pay attention to the maximum number of operations, and other secondary and constant items can be ignored.
L large O notation
(1) algorithm efficiency depends heavily on the number of Operation operations.
(2) When judging, first focus on the maximum number of operations.
(3) estimation of the number of operations can be used as an estimation of time complexity.
O (5) = O (1)
O (2n + 1) = O (2n) = O (N)
O (n2 + n + 1) = O (n2)
O (3n3 + 1) = O (3n3) = O (N3)
Common time complexity types
Relationship: O (1) <O (logn) <O (n) <O (nlogn) <O (n2) <O (N3) <O (2n) <O (N !) <O (NN)
- Best and worst
Example:
# Include <stdio. h>
Int search (INT array [], int length, int N)
{
Int ret =-1;
Int I = 0;
For (I = 0; I <length; I ++)
{
If (array [I] = N)
{
Ret = I;
Break;
}
}
Return ret;
}
Int main ()
{
Int array [5] = {1, 2, 3, 4, 5 };
Printf ("% d \ n", search (array, 5, 1); // o (1), just once
Printf ("% d \ n", search (array, 5, 5); // O (N), N times
Return 0;
}
Meaning:
When the algorithm can still meet the demand in the worst case, it can be inferred that the best and average conditions of the algorithm meet the demand.
Unless otherwise stated, the time complexity of the algorithm we analyze is the worst time complexity.
- Spatial complexity of Algorithms
The space complexity of an algorithm is achieved through the storage space of a computing algorithm.
S (n) = O (f (n ))
Where, n is the problem scale, and F (n) is the function that occupies the storage space when the problem scale is N.
The space complexity indicates the size of the applied space. The large O representation is also applicable to the space complexity of the algorithm. When the space required for Algorithm Execution is a constant, the space complexity is O (1), and N is O (n ).
- Space and time policies
(1) In most cases, the time used for Algorithm Execution is more important.
(2) If necessary, increase the space complexity to reduce the time complexity.
(3) Similarly, you can reduce the space complexity by increasing the time complexity.
L analyze the execution time and space requirements for specific problems during algorithm implementation.
Example:
# Include <stdio. h>
/*
Problem:
In an array composed of numbers ranging from 1 to, each number may appear zero or multiple times.
Design an algorithm to find the number that appears most frequently.
*/
Void search (int A [], int Len)
{
Int SP [1000] = {0 };
Int I = 0;
Int max = 0;
For (I = 0; I <Len; I ++)
{
Int Index = A [I]-1;
SP [Index] ++;
}
For (I = 0; I <1000; I ++)
{
If (max <SP [I])
{
Max = Sp [I];
}
}
For (I = 0; I <1000; I ++)
{
If (max = Sp [I])
{
Printf ("% d \ n", I + 1 );
}
}
}
Int main ()
{
Int array [] = {1, 1, 3, 4, 5, 6, 6, 6, 2, 3 };
Search (array, sizeof (array)/sizeof (* array ));
Return 0;
}
This algorithm is good.
Thoughts:
When the large O notation of the two algorithms is the same, does it mean two computations? Does it mean that the efficiency of the two algorithms is the same?
Data-Lesson 4th-Soul of the trial procedure