Time complexity and space complexity summary

time complexity:

Not a calculation program to calculate the time complexity of the specific running time, the number of execution of the statement but the algorithm.
When there are multiple algorithms before us, we can calculate the time complexity of the algorithm which determines the most cost upon execution time and minimum specific.

Common time complexity are:
Constant order O (1),
of the order of O (log2 n),
the linear order O (n),
the linear logarithmic order O (n log2 n),
the square of the order O (n ^ 2),
the cubic order O (n-^. 3)
K power order O (n ^ K),
exponential order O (2 ^ n).

With increasing n, the time complexity is increasing, the more the algorithm takes time.

Calculation

① select the highest relative increase in items
② highest coefficient is to translate into. 1
③ If the constant words represented by O (1)
such as f (n) = 2 * n ^ 3 + 2n + 100 then the O (n) = n ^ 3 .
Usually we calculate the time complexity is the worst-case calculation

Time computational complexity:

(1) If the execution time of the algorithm is not a problem with the increase in the size of n and growth, even if there are thousands of algorithms statement, the execution time is nothing but a large constant. The time complexity of such algorithms is O (1).

1     int x=1;
2     while (x <10)
3     {
4         x++;
5     }

The algorithm is executed 10 times, is a constant, is represented by the time complexity is O (1).

(2) When there is a plurality of loop, the time complexity of the algorithm is a loop nesting in the most determined frequency f (n) innermost statement.

1  for (i = 0; i < n; i++)
2     {
3         for (j = 0; j < n; j++)
4         {
5             ;
6         }
7     }

The algorithm for loop, each outermost loop executed once every n times the inner loop is executed n times is determined in accordance with, the time complexity is O (n ^ 2).

(3) For the n cycles only, further determination condition related to the execution cycle satisfied.

1 int i=0;
2 while (i < n && arr[i]!=1)
3     {
4         i++;
5     }

In this cycle, if arr [i] is not equal to 1, then, the time complexity is O (n). If arr [i] is equal to 1, then, the cycle can not be performed, the time complexity is 0.

Space complexity

Space complexity of the algorithm is in the process of running a temporary occupation measure of the size of the storage space.

Calculation method:

① ignoring constants, when given O (1)
space ② recursive algorithm complexity of the recursion depth = N * each recursion desired auxiliary space
③ For single-threaded, the runtime stack with a recursive, recursive seeking the deepest that time the number of push-consuming space, because the space that it takes a recursive enough to accommodate all its deepest recursive process.

Space complexity of computing:

1 int a;
2 int b;
3 int c;
4 printf("%d %d %d \n",a,b,c);

Its spatial complexity of O (n) = O (1);

1 int fun(int n,)
2 {
3 int k=10;
4 if(n==k)
5 return n;
6 else
7 return fun(++n);
8 }

Recursive call function fun, always create a variable k. Call n times, the space complexity O (n * 1) = O (n).

Time complexity and space complexity Summary:

Here Insert Picture Description

Published 26 original articles · won praise 17 · views 1342

Guess you like

Origin blog.csdn.net/weixin_44826356/article/details/99488748