[Zero Basic Algorithms Lecture 1] The time complexity of the algorithm

In the computer field, whether it is going to college or an interview, algorithms are a hurdle that cannot be avoided. Some schools and companies account for a large part of the scores in this part of the algorithm. So algorithm learning is very necessary.

This tells us through an example to illustrate what an algorithm is, what is the time complexity of the algorithm, and how to analyze the time complexity of the algorithm. Is it spicy, let's start ↓↓↓

Look at a topic

Obviously random numbers : Obviously, I want to ask some students to do a questionnaire survey in school. For the objectivity of the experiment, he first generated the NN with a computerN random integers between 1 and 1000 (N ≤ 100 N ≤ 100N1 0 0 ), for the repeated number, only one is kept, and the rest of the same number is removed. Different numbers correspond to different student IDs. Then sort these numbers from smallest to largest, and go to the classmates to do the survey in the sorted order. Please help clearly complete the work of "de-duplication" and "sorting".

For this problem, we can use the idea of ​​statistics: set up an array rec[n], and then traverse all student numbers from front to back, and order every time a student number iis encountered rec[i] = 1. Finally traverse the array again rec, as long as the value is 1 is the student ID that has appeared. [Algorithm①]

This kind of statistical thinking is very common in sorting. Interested readers can search for "bucket sorting" or "base sorting"

What is an algorithm

The way to solve the problem with the computer . It is an accurate and complete description of solving a problem.

Our above description of the problem [algorithm①] is an algorithm (but it is not expressed in specific language)

Time complexity: one of the main indicators to measure the quality of an algorithm

Time complexity measures how much time an algorithm uses. The lower the time complexity of an algorithm, the shorter the time it takes to run once, and the better the algorithm. So we have to optimize the time complexity of our algorithm as much as possible on the premise of solving the problem. That is: the same problem can have different algorithms, they may consume different time, we should take the one with the shortest time

Why time complexity is important

Because the time complexity of an algorithm represents how long we need to wait for the algorithm to finish before we can get the solution to the problem.

In a piece of software, a certain algorithm is probably only a small part of the entire system. If this part is used for too long, it may cause problems such as slow response speed of the entire software and poor user experience.

When analyzing data, we need to run a lot of data. At this time, an excellent algorithm can save a lot of time. And an algorithm with too high time complexity may take days, weeks or even years (of course it also has something to do with the hardware)

Therefore, there are certain restrictions on the time complexity of the algorithm in various interview questions.

How to analyze the time complexity of an algorithm

For the question we Kang Kang just now ( obviously random number ), suppose we have a student number randomly out of our hand 1 5 7 1 6 1 4 5 7. We have to do two things: de-duplication and sorting. If we start with the first number 1, search backwards 1, and delete if we find them. Then 5search backwards from the second number 5, and delete when found. (In order to save time, use an del[n]array with an initial value of 0, and the deleted elements iare recorded in the array. del[i] = 1) This problem can also be solved by sorting after this is done. But the time is much slower than the method used in the previous article. [Algorithm ②: De-duplicate first and then sort]

Algorithm ②Time complexity analysis :
de-duplication : For each number, all subsequent numbers must be traversed, and on average, each number needs to be traversed n 2 \frac{n}{2}2nThe total nnumber needs to be traversed. Multiply the two to get the time complexity of this algorithm, expressed as: O (1 2 n 2) = O (n 2) O(\frac{1}{2} n^2) = O(n^2)O (21n2)=O ( n2 )(Because when n is large enough, the influence of the constant can be ignored, so the time complexity usually omit the previous constant)

Sorting : the minimum time complexity is nlog 2 n nlog_2nn l o g2n
(The specific implementation is limited by the length of this article. If you are interested, you can look at "Quick Sort". Many languages ​​also have direct sorting functions that can be used. The time complexity is optimalnlogn nlognn l o g n )logn lognherel o g n refers to the logarithmic level of time complexity, and the base is 2 by default, which is often omitted.

Here, de-duplication and sorting are parallel operations (not affecting each other). We take the largest operation as the time complexity of the entire algorithm. The time complexity of method ② is O (n 2) O(n^2)O ( n2)

Of course, we can also sort the numbers first and then de-duplicate them: 1 5 7 1 6 1 4 5 7after sorting the numbers on hand, we get:, 1 1 1 4 5 5 6 7 7and then starting from the second number, for each number, see if the number before it is equal to it, if it is equal Then delete it (also if you want to save time, you can use an del[n]array to record the deleted elements) [Algorithm ③: sort first and then remove duplicates]

Algorithm ③Time complexity analysis :

Sort : Same as above, the minimum time complexity is nlog 2 n nlog_2nn l o g2n

And deduplication this sorting step on the basis of only the line sweep over the entire array, it is O (n) O (n)O ( n ) level of time complexity. BecauseO (n) < O (nlogn) O(n)<O(nlogn)O ( n ) O ( n l o g n ) , when the two are tied together, we take the largest as its time complexity. The time complexity of method ③ isO (nlog 2 n) O(nlog_2n)O ( n l o g2n)

Algorithm ① time complexity analysis : Algorithm ① only needs to traverse the entire array once, so the time complexity of algorithm ① is O (n) O(n)O ( n ) (If you include the output student ID, iterate twice, but the constant is ignored so it is still linear time complexity)

Comparison of common time complexity

O ( 1 ) < O ( l o g n ) < O ( n ) < O ( n ) < O ( n l o g n ) < O ( n 2 ) < O ( n 3 ) < O ( C n ) < O ( n ! ) < O ( n n ) O(1)<O(logn)<O(\sqrt{n})<O(n)<O(nlogn)<O(n^2)<O(n^3)<O(C^n)<O(n!)<O(n^n) O ( 1 )<O ( l o g n )<O (n )<O ( n )<O ( n l o g n )<O ( n2)<O ( n3)<O(Cn)<O ( n ! )<O ( nn )hereCCC is a constant,nnn is the input size of the algorithm

Their name, just understand: we will O (1) O (1)O ( 1 ) is called constant level,O (logn) O(logn)O ( l o g n ) is called logarithmic order,O (n) O(n)O ( n ) is called the linear level,O (C n) O(C^n)O(Cn ) iscalled exponential,O (n!) O(n!)O ( n ! ) Factorial level. Generally, the algorithms before the linear level are excellent algorithms. After reaching the exponential level, it is very bad.

Frequently used website

The sample questions in this series have corresponding evaluation websites, and the URL will be attached to each question. After reading the blog, readers can link to the corresponding website to submit their own code consolidation. The website is mainly based on cattle , and both Luogu and codeforces are involved.

Guess you like

Origin blog.csdn.net/weixin_44559752/article/details/108218611