(Dry goods) Data structure and algorithm basis

What is a data structure?

Data structure is a way for computers to store and organize data. It refers to a collection of data elements that have one or more specified relationships with each other;

What is an algorithm?

Algorithm (Algorithm) is a well-defined calculation process that takes one or a set of values ​​as input and produces one or a set of values ​​as output; simply put, an algorithm is a series of calculation steps used to convert input data into output ;

Algorithm efficiency

Algorithm efficiency analysis is divided into two types: time efficiency (time complexity) and space efficiency (space complexity); time complexity measures the running speed of the algorithm, and space complexity measures the extra space occupied by the algorithm;
early computer development, storage capacity Very small, so I care about space complexity, but after development, the storage capacity of the computer has reached a very high level, so **We no longer pay special attention to space complexity now;**

Time complexity (time complexity):

The time spent by an algorithm is directly proportional to the number of executions of the statements in it, that is: the number of executions of the basic execution statements in the algorithm is called the time complexity of the algorithm; in
practice, when we calculate the time complexity, we do not need to calculate the exact execution The number of times only needs the approximate number of executions, so here we quote the progressive representation of the big O notation;
from this definition, the following properties of the big O notation can be derived:

(1) For any constant c> 0, there is O(f(n)) = O(c∙f(n))
(2) For any constant a> b> 0, there is O(n a + n b ) = O(n a )

Property (1) means that, in the sense of the big O notation, the positive constant coefficients of the function can be ignored and equal to 1, and
property (2) means that the low-order terms in the polynomial can be ignored, just keep The highest order item;
when the number of executions of the basic execution statement of an algorithm is a given constant, then its time complexity is O(1);
for example:

int count = 0; 
for (int i = 0; i < 2N ; ++ i) 
{
    
     
for (int j = 0; j < N ; ++ j) 
{
    
    
++count; 
}
} 
for (int k = 0; k < N ; ++ k) 
{
    
    
++count;
}
int M = 10; 
while (M--) 
{
    
     
++count; 
}

The number of executions of this code is: 2N 2 +N+10, then converted to the time complexity of Big O , it is: O(N 2 ) It
can be seen that these properties of the Big O notation reflect the overall gradual progress of the function Attention and portrayal of growth trends.

Space complexity (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 the program occupies, because this is not very meaningful, 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.

//The author regularly shares the experience of learning C language, welcome to pay attention

Guess you like

Origin blog.csdn.net/Wyf_Fj/article/details/114128371