The time complexity of the algorithm
Uppercase O () to get the time complexity of the algorithm embodied notation, called big O notation
The method of derivation of large order O
1 substituent constants obtained by operating all the time constants wig
In the number of functions have to run after modification, retaining only the highest order term
If the highest order entry exists and is not 1, the removal and the constant term obtained by multiplying
We have to get the final result is too large O-order
1 " constant-order
Example: the code segments are much too large O
int sum = 0, n = 100;
printf(“I love you.com\n”);
printf(“I love you.com\n”); printf(“I love you.com\n”); printf(“I love you.com\n”); printf(“I love you.com\n”); printf(“I love you.com\n”); sum = (1+n)*n/2;
The first adder to describe all constants are O (1)
2》线性阶
int i , n = 100, sum = 0;
for( i=0; i < n; i++ ) { sum = sum + i; }
Time complexity of O (n-)
. 3 "order of the square
int i, j, n = 100;
for( i=0; i < n; i++ )
{
for( j=0; j < n; j++ ) { printf(“I love FishC.com\n”); } }
Time complexity of O (n ^ 2)
Summary: If there are three nested loops so obtained is n ^ 3. Summarizes results, the loop will have time complexity equal to the complexity of the loop body obtained by multiplying the number of cycles run
int i, j, n = 100;
for( i=0; i < n; i++ )
{
for( j=i; j < n; j++ ) { printf(“I love FishC.com\n”); } }
n-+ (n-1) + (n--2) + ... + 1 = n- (n-+. 1) / 2
n-(n-+. 1) / 2 = ^ n-2/2 + n / 2
with a large O Raiders we derive only the most items, it is n / 2 this is removed. Third, removing the highest and multiplied by a constant term, to give the final O (n ^ 2).
int i = 1, n = 100;
while( i < n )
{
i = i * 2; }
Because after every i * 2, n is more closer to the distance, assuming a 2 by multiplying x is greater than or equal to n, it will exit the loop.
Then the obtained 2 ^ x = n x = log (2) n, so that the cycle time complexity is O (logn).
Function to give the time complexity analysis
Have common time-consuming to obtain the time complexity from small to large
n! = 1 * 2 * 3 * 4 * ...... * n (n digits)
O(1)<O(logn)<O(n)<O(nlogn)<O(n^2)<O(n^3)<O(2^n)<O(n!)<O(n^n)
Algorithms have space complexity
Space complexity algorithms have available storage space required by the time calculation algorithm, the algorithm to obtain the spatial complexity calculated S (n) = O (f (n)), where n is the size of the problem too, F (n) is a statement about n storage space was occupied function
Usually refers to the time complexity of running time too needs to refer to the spatial complexity space requirements