Page learns programming | complexity analysis turns out to be so simple

Page learns programming | complexity analysis turns out to be so simple

Page learns programming | complexity analysis turns out to be so simple
Page learns programming | complexity analysis turns out to be so simple
Page learns programming | complexity analysis turns out to be so simple
Page learns programming | complexity analysis turns out to be so simple
1. What is the data structure used for?

The birth of data structures and algorithms allows computers to "execute faster" and "save space".

2. What is used to judge the quality of data structures and algorithms?

Judge the quality of the data structure and algorithm from the two aspects of "execution time" and "occupied space".

3. What is complexity?

Use "time complexity" and "space complexity" to describe performance issues, and both are collectively referred to as complexity.

4. What does complexity describe?

Complexity describes the relationship between algorithm execution time (or space occupied) and data size growth.
Page learns programming | complexity analysis turns out to be so simple

1. What are the advantages compared to performance analysis?

The degree of assistance analysis has the characteristics of not relying on the execution environment, low cost, high efficiency, easy operation, and strong guidance.

2. Why is complexity analysis required?

Complexity describes the relationship between algorithm execution time (or space occupied) and data size growth.

Page learns programming | complexity analysis turns out to be so simple
1. What methods can perform complexity analysis?

Method: "Big O Notation"

2. What is Big O notation?

The "execution time" of the algorithm is directly proportional to the "number of executions" of each line of code [T(n) = O(f(n))] = "where T(n) represents the total execution time of the algorithm, and f(n) represents each The total number of lines of code execution, and n often indicates the size of the data.

3. What are the characteristics of Big O notation?

Since time complexity describes the growth trend of algorithm execution time and data scale, constant order, low order, and coefficients actually have no decisive influence on this growth trend, so these items are ignored when doing time complexity analysis.

4. Complexity analysis rules

  • [Single code to see frequency]: See the time complexity of the "loop code" in the code snippet.

  • [Multiple pieces of code to see the largest]: If there are multiple for loops, look at the time complexity of the code with the "most nested loops".

  • [Nested code product]: Loop, recursive code, multiply the inner and outer nested codes to reduce the time complexity.

------------------❤------------------

time complexity

1. What is complexity?

The "execution time T(n)" of all codes is proportional to the "number of executions n" of each line of code [T(n) = O(f(n))].

2. Three methods of analysis

■ Maximum rule

Ignore the constants, low-levels, and coefficients in the formula, and just take the maximum number of cycles, which is the line of code with the most cycles.

Example

1// 求n个数字之和
2int xiaolu(int n) {
3   int sum = 0;
4   for (int i = 1; i <= n; ++i) {
5     sum = sum + i;
6   }
7   return sum;
8 }

analysis


The second line is a line of code, which is a constant level, which has nothing to do with n and can be ignored. The fourth and fifth lines of code are our key analysis objects, and are related to n. Time complexity reflects the relationship between execution time and n data scale. It needs to be executed n times to find the sum of n data. So the time complexity is O(n).

■ Rule of addition

The total complexity is equal to the complexity of the cycle with the largest number of cycles.

Example


 1int xiaolu(int n) {
 2   int sum = 0;
 3   //循环一
 4   for (int i = 1; i <= 100; j++) {
 5     sum = sum + i;
 6   }
 7   //循环二
 8   for (int j = 1; j <= n; j++) {
 9      sum = sum + i;
10   }
11 }

analysis


There are two loops above, one loop 100 times, and the other loop n times. We choose the loop with the largest number of loops and is related to "data size n". It can be seen from the above that we can easily select the second cycle, which is related to the data size n, the number of cycles is the largest, and the time complexity of the code with the largest number of cycles represents the overall time complexity, which is O(n);

■ Multiplication rule

When we encounter nested for loops, how do we calculate the time complexity? That is the product of the inner and outer cycles.

Example


1 for (int j = 1; j <= n; j++) {
2     for(int i = 1; i <= n; i++)
3     sum = sum + i;
4 }

analysis


The outer loop is once, the inner loop is n times, then the outer loop is n times, and the inner loop is n*n times. So the time complexity is O(n²).

Space complexity

1. What is space complexity?

Represents the growth relationship between the algorithm's "storage space" and "data size"

Example


 1int xiaolu(int n) {
 2   int sum = 0;
 3   //循环一
 4   for (int i = 1; i <= 100; j++) {
 5     sum = sum + i;
 6   }
 7   //循环二
 8   for (int j = 1; j <= n; j++) {
 9      sum = sum + i;
10   }
11 }

analysis


In all the codes, we can easily find the code related to the storage space. The second line applies for a storage space of n size, so the space complexity is O(n).

2. The most common space complexity

O (1) 、 O (n) 、 O (n²)。

■ O (1)

The time complexity representation method of the constant level, whether it is a line of code or multiple lines, as long as it is a constant level, it is represented by O(1).

Example


1int i = 1;
2int j = 2;
3int sum = i + j;

analysis


Because these three lines of code, that is, the constant level code, do not change with the change of the n data size. (Except for loop and recursion)

■ O (logn) | O (nlogn)

"Logarithmic time complexity" is the most difficult time complexity to analyze.

Example


1 i=1;
2 while (i <= n)  {
3   i = i * 3;
4 }

analysis


The time complexity required for this code is how many times this code has been executed. See the figure below for specific analysis.

Page learns programming | complexity analysis turns out to be so simple

supplement


Regardless of whether it is based on 2, 3, or 10, the time complexity of all logarithmic orders can be recorded as O(logn), because the logarithm can be converted, refer to high school textbooks.

* ■ O (m + n) | O (m n) **

Refer to the addition and multiplication rules mentioned above.

Page learns programming | complexity analysis turns out to be so simple

Page learns programming | complexity analysis turns out to be so simple
Page learns programming | complexity analysis turns out to be so simple
1. The best and worst time complexity

The so-called best and worst time complexity correspond to the code execution in the best case and the worst case respectively.

Example


1 //在一个 array 数组中查找一个数据 a 是否存在
2for (int i = 1; i < n; i++) {
3    if (array[i] == a) {
4       return i;
5    }
6 }

analysis:


1. The best case is that the first one in the array is the data we want to find. The above code can be executed once. In this case, the time complexity is the best time complexity, which is O(1).

2. The worst case is that the last of the array is the data we want to find, and we need to loop through the array n times, which corresponds to the worst time complexity of O(n).

2. Average time complexity

The average time complexity needs to be analyzed with the knowledge of probability theory, which is what we call the weighted average in probability theory, also called expected value.

analysis


For example, in the example above, suppose that the probability of the data we are looking for in the array is 1/2; the probability of appearing in the array is n/1. According to the formula below, the probability of appearance can be calculated as 1/2n.

Page learns programming | complexity analysis turns out to be so simple

Then we consider each situation to calculate the average time complexity.

Page learns programming | complexity analysis turns out to be so simple

3. Amortized time complexity

■What is amortized time complexity?

For example, the time complexity of inserting data every n times is O(1), and the time complexity of inserting data once is O(n). We average the time complexity of this time to n times of inserting data, and the time The complexity is still O(1).

■ Amortization analysis

For example, the time complexity of inserting data every n times is O(1), and the time complexity of inserting data once is O(n). We average the time complexity of this time to n times of inserting data, and the time The complexity is still O(1).

■ Applicable scene

Generally applied to a certain data structure, the time complexity of continuous operation is relatively low, but the time complexity of individual cases is particularly high, we will average the particularly high this time to the lower operation.

■ Performance comparison of several complexity

Page learns programming | complexity analysis turns out to be so simple

Page learns programming | complexity analysis turns out to be so simple

Guess you like

Origin blog.51cto.com/15064450/2602241