Key concepts
To analyze the complexity of algorithms, we usually need to analyze the running of loops.
1. If the complexity of a loop is O (1), the time complexity of the loop is O (n ).
For (INT I = 0; I <n; I ++ ){
// Some steps with column complexity of O (1 ....
}
Generally, if a loop structure runs n times linearly and the time complexity of the loop body is O (1), the complexity of the loop is O (n ).
Even if the loop skips some constant parts, as long as the Skip part is linear, the time complexity of the loop body is still O (n ).
For example
Int COUNT = 1;
While (count <n ){
Count + = 2;
// Some steps with column complexity of O (1 ....
}
Time complexity or O (N)
2. If the complexity of the loop body is logarithm-level
Int COUNT = 1;
While (count <n ){
Count * = 2;
// Some steps with column complexity of O (1 ....
}
This cycle is O (logn), usually 2 is the bottom, that is, O (log2n)
Key concepts
The time complexity of the loop is equal to the complexity of the loop body multiplied by the number of cycles...
3. nested loop complexity analysis...
For (INT count1 = 0; count1 <n; count1 ++ ){
For (Int ?? Count2 = 0; count2 <n; count2 ++ ){
// Some steps with column complexity of O (1 ....
}
}
In this case, we should first calculate the time complexity of the inner loop, and then multiply the complexity of the inner layer by the number of times of the outer loop.
The time complexity of the inmost loop body is O (1), so N cycles are multiplied by N times of the outermost layer.
So it is concluded that the time complexity of Layer 2 nested loops = O (1) * n = O (n2)
When analyzing the complexity of nested loops, both the inner loop and the outer loop will be taken into consideration.
Iv. Complexity Analysis of method calls
If the following code is available:
For (int count = 0; count <n; count ++ ){
Printsum (N );
}
The order of the loop is equal to the order of the loop body multiplied by the number of cycles. In this case, the loop body is called by a method, so what is the time complexity of the loop body!
This method is to print 1 ~ So the time complexity of the method body must be calculated first.
Public void printsum (INT count ){
Int sum = 1;
For (INT I = 0; I sum + = I;
}
System. Out. Print (SUM );
}
Remember, only runable statements increase the time complexity. Therefore, except for loops, the complexity of other runable statements is O (1 ),
So the time complexity of printsum = for O (n) + O (1) = ignore constant = O (N)
But in retrospect, we asked the program to print 1 ~ N and do not need to use for loop remember that the teacher gave a formula num = N * (n + 1)/2
Change
Public void printsum (INT count ){
Int sum = 1;
Sum = count * (count + 1)/2;
System. Out. Print (SUM );
}
In this case, the order of the printsum method is O (1) --------> which means that the loop complexity of the outermost layer calling this method is improved from O (n2) to O (n)
This is a big improvement. From this point we can see the difference between simple algorithms and efficient algorithms.
5. If a method body is composed of multiple method calls and multiple loops, what is the complexity!
Public void suixiangmethod (int n ){
Printsum (n); // 1.1.
For (INT I = 0; I printsum (N );
}
For (INT I = 0; I for (int K = 0; k
System. Out. Print (I, K );
}
}
Is the time complexity of the suixiangmethod method required to calculate the complexity of each member of the method body?
That is, 1.1 + 1.2 + 1.3 = O (1) + O (n) + O (n2) ----> ignore constants and non-primary items = O (n2)