This article is included in the album:http://dwz.win/HjK
Hello, I’m brother Tong, a hard core man who climbs 26 floors every day and doesn’t forget to read the source code.
As we all know, the main problem solved by data structure and algorithm is “fast” and “save”, that is, how to make the code run faster and save more storage space.
Therefore, “fast” and “save” are two very important indicators to measure an algorithm, that is, we often hear the analysis of time complexity and space complexity.
So why do you need complexity analysis? What is the methodology of complexity analysis?
This is the problem we are going to solve in this section.
OK, let’s go into today’s study.
Why complexity analysis?
First of all, let’s consider a question: for the two algorithms, how can we judge who runs faster and who saves more memory?
You may say that it’s not easy. Just run these two algorithms and count the running time and memory?
Yes, it’s a good way, and it has a very vivid Name:Post event statistics。
However, this statistical method has obvious problems
Different input has a great influence on the result
For some inputs, algorithm a may perform faster; for others, algorithm B may perform faster. For example, in the sorting algorithm we will learn later, the order of input has completely different effects on different sorting algorithms.
Different machines have a big impact on the results
For the same input, algorithm a may be faster on one machine and algorithm B may be faster on another. For example, algorithm a can use multiple cores, but algorithm B can’t, so the impact of CPU cores on the two algorithms will be quite different.
The size of the data has a big impact on the results
When the data size is small, algorithm a may be faster, while when the data size is large, algorithm B may be faster. For example, the sorting algorithm we will learn later, when the data size is relatively small, insert sorting is faster than merge sorting.
Therefore, we need a method that can estimate the efficiency of the algorithm without actually running the algorithm.
This is what we call complexity analysis.
So, how to analyze the complexity? Is there any methodology?
This methodology is calledAsymptotic analysis。
What is asymptotic analysis?
Asymptotic analysis method is to link the efficiency of algorithm execution with the scale of input. With the increase of input scale, what kind of trend will the time (or space) required for algorithm execution present? This trend is called asymptotic analysis methodasymptoticAnd this method is calledAsymptotic analysis。
The concept may be rather awkward. Let me give a simple example. For a given ordered array, I want to find the location of a value. For example, what are the methods to find the element 8?
Simple violence point method, traversal from the beginning, find the element is returned.
A more friendly method is to use dichotomy. Each time you locate to the middle of the data, you can see the size of its value and the target value, and judge whether it is on the left or right. Continue to search in dichotomy.
The input size of the above example is an ordered array of 8 elements, and the target value is 8. The second method is much faster than the first method.
But what if the target of the search is 1?
For the first method, one search is enough.
For the second method, you need to look up three times.
At this time, the second method is inferior to the first method.
Therefore, comparing the efficiency of the two algorithms, we should not only consider individual elements, but also consider the feelings of all elements.
We use mathematical method to calculate the average execution efficiency of the two methods, assuming that the input scale is expanded to n.
For the first method, element 1 is searched once, element 2 is searched twice, and element 3 is searched three times And the probability of finding each element is 1 / n.
Therefore, its execution efficiency is: 1×1 / N + 2×1 / N + 3×1 / N +… NX1 / N = NX (n + 1) / 2 / N = (n + 1) / 2.
For the second method, one element in the middle is searched once, two elements in the middle are searched twice, four elements in the middle are searched three times, the scale of each search is reduced by half, and the probability of searching each element is 1 / n.
Therefore, its execution efficiency is: 1x1x1 / N + 2x2x1 / N + 4x3x1 / N +… + 2 ^ (log2 (n) – 2) x (log2 (n) – 1) x 1 / N + 2 ^ (log2 (n) – 1) x log2 (n) x 1 / n =?
I went. What’s the result?
It’s time to show real strength:
You may want to curse me. As a primary school graduate, can’t I learn data structure and algorithm?
No, no, no, I can’t play like this. So, how should I play? Let’s go on to the next section.
In this section, we explain why we need complexity analysis from the aspect of algorithm execution efficiency, and introduce the method of complexity analysis, that is, asymptotic analysis method. If we strictly follow the asymptotic analysis method, we need a lot of mathematical knowledge, which undoubtedly increases the difficulty of our analysis algorithm. Then, is there any method to calculate complexity more easily?
Pay attention to the official account “Tong brother read the source code”, unlock more source code, infrastructure and architecture knowledge.