Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Analysis of the best, worst, average, and amortized time complexity

1. Four concepts of complexity analysis


1. Worst-case time complexity: the time complexity of code execution in the most ideal case.
2. Best case time complexity: the time complexity of code execution in the worst case.
3. Average time complexity: expressed by the weighted average of the number of times the code
is executed in all cases.
4. Amortized time complexity: Most of the complexity of code execution is low-level
complexity, and some cases are high-level complexity and timing relationships occur. Individual
high-level complexity can be evenly amortized to low-level complexity. Basically, amortized
result is equal to low-level complexity.

2. Why are these 4 concepts introduced?


1. The time complexity of the same piece of code will vary in magnitude under different
circumstances. In order to describe the time complexity of the code more comprehensively and
accurately, these 4 concepts are introduced.
2. The four types of complexity need to be distinguished only when the code complexity differs
in magnitude under different circumstances. In most cases, there is no need to analyze them
separately.

3. How to analyze the average and amortized


time complexity?
1. Average time complexity
The magnitude of the difference in the complexity of the code in different situations is expressed
by the weighted average of the execution times of the code in all possible situations.

2. Amortized time complexity


Use when two conditions are met: 1) The code is low-level complexity in most cases, and only a
few cases are high-level complexity; 2) Low-level and high-level complexity appear with timing
laws. The amortized result is generally equal to low-level complexity.

Fourth, think after class


Analyze the time complexity of the following functions:
1. // Global variable, an array of size 10, length len, subscript i.
2. int array[] = new int[10];
3. int len = 10;
4. int i = 0;
5.
6. // add an element to the array
7. void add(int element) {
8. if (i >= len) {// not enough space in the array
9. // Re-apply for an array space of 2 times the size
10. int new_array[] = new int[len*2];
11. // Copy the data in the original array to new_array in turn
12. for (int j = 0; j < len; ++j) {
13. new_array[j] = array[j];
14. }
15. // copy new_array to array, the size of array is now 2
times len
16. array = new_array;
17. len = 2 * len;
18. }
19. // Place the element at the position where the subscript is i,
and add one to the subscript i
20. array[i] = element;
21. ++i;
22. }

Answer analysis:
1. The best case time complexity is O(1)
2. Worst-case analysis:
The worst-case code execution times are related to the length of each array
The number of executions of the first call to insert is n,
The number of executions of the second call to insert is 2n,
The number of executions of the third call to insert is 2^2 * n
The number of executions of the kth call to insert is 2^(k-1) * n
The worst time complexity is O(n).
3. Average situation analysis
Each time the worst case is encountered, the array will be expanded twice. The original array is
imported into the new array. Although the length of the array becomes larger, the length of the
interval in which the insertion operation falls They are the same, they are 0~len-1, len~(2len-
1),...;
The insertion situation is still len+1: 0~len-1 and O(len) after the insertion; so the probability of
each insertion is: p = 1/len+1 ,
Finally, the weighted average time complexity is calculated as 1*p + 2*p+ + len*p + len * p =
O(1);
4. Amortized time complexity O(1)
And the amortized complexity is because each occurrence of O(len) is followed by len times
O(1), which is coherent, so O(len) is evenly distributed to the previous len times. , The amortized
complexity is O(1)

You might also like