"The Beauty of Data Structures and Algorithms" Study Notes Day 3

Course: "Complexity Analysis (Part 2): A Brief Analysis of the Best, Worst, Average, and Amortized Time Complexities" Summary

Sometimes, the time complexity of the code will differ in magnitude under different circumstances. In order to describe the time complexity of the code more comprehensively and accurately, the following concepts need to be introduced.

Four concepts of complexity analysis

  1. Best case time complexity
    The time complexity of the code executing under the most ideal situation.
  2. worst case time complexity
    The time complexity of the code executing in the worst case.
  3. Average case time complexity (average case time complexity)
    The weighted average of the complexity of the code in all cases, that is, The weighted average Time complexity or Expected time complexity
  4. Amortized time complexity (amortized time complexity)
    In a group of continuous operations with timing relationships, if this group of operations is regarded as a whole, then it can Amortize the execution time of the higher complexity code to the lower complexity code. Then, after conducting complexity analysis, the result is averaged time complexity. This analysis method is called amortized Analysis. Generally in this special situation, the amortized time complexity is the best-case time complexity.

The following is a case analysis of the after-school questions:

// 全局变量,大小为10的数组array,长度len,下标i。
int array[] = new int[10]; 
int len = 10;
int i = 0;

// 往数组中添加一个元素
void add(int element) {
   if (i >= len) { // 数组空间不够了
     // 重新申请一个2倍大小的数组空间
     int new_array[] = new int[len*2];
     // 把原来array数组中的数据依次copy到new_array
     for (int j = 0; j < len; ++j) {
       new_array[j] = array[j];
     }
     // new_array复制给array,array现在大小就是2倍len了
     array = new_array;
     len = 2 * len;
   }
   // 将element放到下标为i的位置,下标i加一
   array[i] = element;
   ++i;
}
  • Best case time complexity
    The best case is that there is enough space in the current array to store the added data, that is, the adding data operation is directly performed, so the total number of execution times of the code is Constant level, so the best time complexity is O(1).

  • Worst case time complexity
    The worst case is that the array is full, and you need to re-apply twice the new array space and loop to copy the elements of the current array to the new array. Assuming that the length of the current array is n, according to the Big O complexity analysis method, the time complexity of the code can be O(n).

  • Average case time complexity
    Analysis shows that the possible storage locations of the added elements are0, 1, 2, 3, 4...n -1 Thisn situation, as well as the situation when the array is full and requires new application space, that is, a total of1/(n+1), assuming that the probability of each situation is the same, that is, O(n), and the time complexity (that is, the worst time complexity) when applying for space is O(1) The complexity is n case, the time in the previous case where no application for space is requiredn+1the average time complexity is: T(n) = n cases ( O(1) x 1 n + 1 {1 \over n + 1}
    n+11 ) + ( O(n) x when the array is full 1 n + 1 {1 \over n + 1} n+11 ) 即:

    T(n) = O(1) x 1 n + 1 {1 \over n + 1} n+11 + O(1) x 1 n + 1 {1 \over n + 1} n+11 … O(1) x 1 n + 1 {1 \over n + 1} n+11 + O(n) x 1 n + 1 {1 \over n + 1} n+11 = O( 2 n n + 1 {2n \over n + 1} n+12n) = O(1)

  • Amortized time complexity
    Analysis of the code shows that every time n timesO(1) operation will be followed by an operation of O(n). If Considering these operations as a whole, the average time spent on this operation of O(n) is n timesO(1) operations, which is equivalent to one more code execution< a i=14>O(1) operation, that is, O(1) + O(1) = O(2) = O(1)< /span>.

  • The following is my analysis based on the meaning of time complexity:
    We know Big O time complexity represents the changing trend of code execution time as the data size grows. From this perspective, look at this code. When the size of the array is large and there is enough space to store the added data, the number of execution times of the code will be constant. Therefore, the averaged time complexity is O(1), and the averaged time complexity is also O(1) a>.


Previous article: "The Beauty of Data Structures and Algorithms" Study Notes Day 2
Next article:"The Beauty of Data Structure and Algorithm" Study Notes Day 4

Guess you like

Origin blog.csdn.net/weixin_44131612/article/details/129133681