time complexity space complexity


introduction

        Time complexity and space complexity are a judging tool used to measure the quality of its algorithm, please continue to watch for details.

Not much to say, fasten your seat belt, let's start the car (recommended to watch on computer) .

        Attachment: red, the part is the key part; the blue color is the part that needs to be memorized (not rote memorization, knock more); black bold or other colors are the secondary key points; black is the description need

mind Mapping:

If you want XMind mind map, you can private message

1. Time complexity        

Knowledge points:

Time Complexity: In computer science, the time complexity of an algorithm is a function that quantitatively describes the running time of that algorithm. Theoretically speaking, the time it takes for an algorithm to execute cannot be calculated. Only when you put your program on the machine and run it can you know it. But do we need to test each algorithm on the computer? It is possible to test them all on the computer, but it is very troublesome, so there is an analysis method of time complexity. The time an algorithm takes is proportional to the number of times the statements in it are executed. ( Simply speaking, the calculation of time complexity is not to calculate the time it takes, but to calculate the number of executions during its runtime )

Details (points to note):

How to calculate the time complexity of an algorithm:

We generally use the big :

  1. For the time complexity of constant times , it is directly represented by 1: O(1)
  2. When calculating the time complexity, we only take the approximate magnitude (take the largest magnitude)
  3. For an N * constant , we can directly omit the multiplied constant times: O(N) (for an N, multiplying a constant has little effect on the data)
  4. When calculating the time complexity, we generally calculate his worst case directly (because some time complexity cannot be directly calculated, it may be divided into the best and worst cases)
  5. The logarithm of N whose log is base 2 is generally written as direct logN

practise:

Example 1:

void Func2(int N)
{
     int count = 0;
     for (int k = 0; k < 2 * N ; ++ k) // 2 * N == N
     {
         ++count;
     }
     int M = 10;
     while (M--)// O(1)
     {
         ++count;
     }
     printf("%d\n", count);
}

The above Fun2 time complexity: O(N)   (generally written in this form: ...... The time complexity is: O(...) )

 Example 2:

void Func4(int N)
{
 int count = 0;
 for (int k = 0; k < 100; ++ k)//常数次
 {
 ++count;
 }
 printf("%d\n", count);
}

//Fun4的时间复杂度:O(1)

Example 3:

void Func3(int N, int M)
{
 int count = 0;
 for (int k = 0; k < M; ++ k)
 {
 ++count;
 }
 for (int k = 0; k < N ; ++ k)
 {
 ++count;
 }
 printf("%d\n", count);
}

//

Because I don't know the relationship between MN, I write it as O(M+N)

If M >> N : O(M) 

Otherwise: O (N)

If M == N : O(N) / O(M)


2. Space complexity

Knowledge points:

Space complexity is also a mathematical expression, which is a measure of the amount of storage space temporarily occupied by an algorithm during operation . Space complexity is not how many bytes the program occupies, because this is not very meaningful, so space complexity is calculated by the number of variables . The space complexity calculation rules are basically similar to the time complexity, and the big O asymptotic notation is also used .

detail:

  1. The stack space (storage parameters , local variables, some register information, etc.) required by the function at runtime has been determined during compilation, so the space complexity is mainly determined by the extra space explicitly requested by the function at runtime (that is, the function 's The parameters are generally not counted in the space complexity of the function )
  2. Generally, the number of variables can be directly counted, unless additional space is opened up (malloc(n * sizeof(int))

practise:

Example 1:

void BubbleSort(int* a, int n)
{
 assert(a);
 for (size_t end = n; end > 0; --end)
 {
     int exchange = 0;
     for (size_t i = 1; i < end; ++i)
     {
         if (a[i-1] > a[i])
         {
             Swap(&a[i-1], &a[i]);
             exchange = 1;
         }
     }
 if (exchange == 0)
     break;
 }
}
//时间复杂度是:O(N ^ 2) : n - 1 + n - 2 + ... + 1 = n(a1+an)/2 = n ^ 2

//空间复杂度是:O(1): 总共就3个变量

Example 2:

long long* Fibonacci(size_t n)
{
 if(n==0)
 return NULL;
 
 long long * fibArray = (long long *)malloc((n+1) * sizeof(long long));
 fibArray[0] = 0;
 fibArray[1] = 1;
 for (int i = 2; i <= n ; ++i)
 {
 fibArray[i] = fibArray[i - 1] + fibArray [i - 2];
 }
 return fibArray;
}
//空间复杂度是:O(N) : malloc 开辟了一个新的空间 大约是N个 (这里并不关心byte)

Example 3:

long long Fac(size_t N)
{
 if(N == 0)
 return 1;
 
 return Fac(N-1)*N;
}
//因为递归,每个递归建立的栈帧都要创建变量,所以
//空间复杂度是: O (N)

Example 4:

long long Fib(size_t N)
{
 if(N < 3)
 return 1;
 
 return Fib(N-1) + Fib(N-2);
}

Its space complexity is: O(N), time complexity is: O(2^N)
because:

 Knowledge point: After the stack frame space is returned to the operating system, the next stack frame may occupy the same area.

 

This chapter is over. To predict what will happen next, let's wait for the next chapter to break it down.

Continuously update a large amount of data structure and detailed content, pay attention to it three times

Guess you like

Origin blog.csdn.net/ZYK069/article/details/130026754