Data structure study notes (1. Basic knowledge and time and space complexity)

Data structure is a way of computer storage and organization of data. It refers to a collection of data elements that have one or more specific relationships with each other.
An algorithm is a well-defined calculation process. It takes one or a set of values ​​as input and produces one or a set of values ​​as output. In simple terms, an algorithm is a series of calculation steps used to convert input data into output results. .
Algorithm efficiency
Algorithm efficiency analysis is divided into two types: the first is time efficiency, and the second is space efficiency. Time efficiency is called time complexity, and space efficiency is called space complexity. Time complexity mainly measures the running speed of an algorithm, while space complexity mainly measures the extra space required by an algorithm. In the early days of computer development, the storage capacity of a computer was very small. So I really care about space complexity. But after the rapid development of the computer industry, the storage capacity of computers has reached a very high level. So we no longer need to pay special attention to the space complexity of an algorithm.
1. Time complexity
definition: In computer science, the time complexity of an algorithm is a function that quantitatively describes the running time of the algorithm. The time it takes for an algorithm to execute, theoretically, cannot be calculated, and you can only know if you put your program on the machine and run it. But do we need to test every algorithm on the computer? It can be tested on the computer, but this is very troublesome, so the analysis method of time complexity is available. The time spent by an algorithm is proportional to the number of executions of the statements in it. The number of executions of the basic operations in the algorithm is the time complexity of the algorithm.

Big O progressive notation
1. Replace all additive constants in the running time with a constant 1.
2. In the modified running frequency function, only the highest order items are retained.
3. If the highest-order term exists and is not 1, remove the constant multiplied by this term. The result is a big O order.

Example:

long Fib(size_t N) 
{
return N < 2 ? N : Fib(N-1)+Fib(N-2);
}
分析发现基本操作递归了2^N次,时间复杂度为O(2^N)。

2. Space complexity
Space complexity is a measure of the amount of storage space that an algorithm temporarily occupies during its operation. Space complexity is not how many bytes of space the program occupies, because this does not make much sense, so space complexity is the number of variables. The space complexity calculation rules are basically similar to the time complexity, and the big O progressive notation is also used.
example:

long Fac(size_t N) 
{
 return N < 2 ? N : Fac(N-1)*N; 
 }
 分析发现递归调用了N次,开辟了N个栈帧,每个栈帧使用了常数个空间。空间复杂度为O(N)

Guess you like

Origin blog.51cto.com/14289397/2545647