[C / C ++ syntax] - time complexity and space complexity

We look at the quality of the algorithm mainly from the time and space to measure the pros and cons of the two angles algorithm
Time: execution time of the algorithm is
space: the algorithm is the temporary memory size

time complexity

We usually use a large O notation indicates time complexity, because the number of times of execution of each line of code and execution time proportional to the same computer, let's take an example

int ans = 0for(int i = 1; i <= n; i++)
{
	ans += i;
}

Assuming the same code execution time of each row, the number of executions. 1 + + code. 3. 1 n. 3 +. 1 = (n +. 1), this algorithm is time-consuming changes with changes in n, so we can be simplified the time complexity of the algorithm is expressed as: O (n), so Why can simplify it, because the big O notation does not come true representative of the execution time for the algorithm, which is used to indicate changes in code execution time of growth trends. So in the example above, if n is infinite and time is not constant multiple of significance.
Common time complexity are:
time complexity is increasing, the lower the efficiency of the algorithm execution
Here Insert Picture Description

Seek time complexity

⑴ find basic statements algorithm;

Algorithm to perform the highest number of that statement is the basic statement, usually the innermost loop cycle.

⑵ calculated number of times of execution of the basis statement level;

Just calculate the number of executions basic statement of magnitude, which means that as long as a function of the number of executions of basic statement of the right to the highest power, you can ignore all the low power factor and the highest power. This can simplify the analysis of algorithms, and the focus on the most important point: the growth rate.

⑶ represents time performance of the algorithm with a large Ο mark.

The basic statement is executed into a large number of orders of magnitude Ο notation.

Constant order O (1)

No matter how many lines of code executes, as long as no complicated structure cycle or the like, executing code to determine the number of lines number, that the time complexity of this code are to O (1), such as:

int i = 1;
int j = 2;
++i;
j++;
int m = i + j;
m++

Linear order O (n)

One loop order linear code as in the above example

int ans = 0for(int i = 1; i <= n; i++)
{
	ans += i;
}

Their time complexity is O (n);

Of the order O (log n- )

Look at the code

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

Are doubled for each cycle i, n i from getting near it, after assuming x times greater than or equal i n, 2 x > n = 2 then x = log logarithm to the base n of the
time complexity can be abbreviated as O (log n- ) ;

Linear order of O (log n-* n- )

The time complexity is O (logn) cyclic code N times, then it is the time complexity of n * O (logN), it wants to O (nlogN).

for(m=1; m<n; m++)
{
    i = 1;
    while(i<n)
    {
        i = i * 2;
    }
}

O (N 2 )及o (N 3 )

2 or 3 layers of nested loop

Time complexity of the algorithm used

Here Insert Picture Description
It should be noted
Here Insert Picture Description

Space complexity

Space complexity is defined as the algorithm temporary memory space occupied, the title usually 128MB
when an algorithm of the spatial complexity is a constant that does not vary with the processing amount data n of size change can be expressed as O (1).
When the space complexity of the algorithm and a time proportional to the logarithm to the base 2 of n, can be expressed as 0 (10g2n);
when a spatial relationship with the complexity of the algorithm is linearly proportional to time n can be expressed as 0 (n .) If the parameter is an array, only need to allocate a memory space by the argument of a transmitted address pointer, i.e. a machine word space for it;
if the parameter is a reference, it is only required to assign the storage an address space, use it to store the address of the corresponding argument variables, referenced by the system to automatically variable argument.

Published 20 original articles · won praise 2 · Views 942

Guess you like

Origin blog.csdn.net/zhbbbbbb/article/details/103456136