Time complexity and space complexity are enough!

Summary of time complexity and space complexity

1. Algorithm efficiency

Algorithm efficiency is divided into two types, time efficiency and space efficiency, namely time complexity and space complexity. The former measures the speed of the algorithm and the latter measures the extra space required by the algorithm.

2. Time complexity

2.1 Basic concepts
The time complexity in an algorithm is a function that quantitatively describes the running time of the algorithm. The
summary is: the number of executions of the basic operations of the algorithm.
Focus : the order of magnitude of the operation/the number of executions of the basic operation. The
execution time is strongly related to hardware resources. , The processing speed of different hardware is different
②The progressive representation of the 2.2 big O that the CPU performs operations per second above 100 million

// 请计算一下Func1基本操作执行了多少次?
void Func1(int N) 
{
    
    
int count = 0;
for (int i = 0; i < N ; ++ i) 
{
    
    
 for (int j = 0; j < N ; ++ j)
 {
    
    
 ++count;
 }
}
for (int k = 0; k < 2 * N ; ++ k)
{
    
    
 ++count;
}
int M = 10;
while (M--) 
{
    
    
 ++count; 
}
printf("%d\n", count);
}

//Func1 执行的基本操作次数 :
N = 10 F(N) = 130
N = 100 F(N) = 10210
N = 1000 F(N) = 1002010
实际中我们计算时间复杂度时,我们其实并不一定要计算精确的执行次数,而只需要大概执行次数,那么这里我们使用大O的渐进表示法。

Big O notation: It is a mathematical symbol used to describe the progressive behavior of a function.
The method of deriving the big O order:
1. The highest order term has a coefficient, and the coefficient is ignored.
2. If the number of executions is constant, it is O(1)
3. If the highest order term exists and is not 1, remove the multiplied item constant. The result is a big O order.
4. You can't rely on the nested simple leg breaking time complexity of loops, and you should analyze the number of executions of basic operations. After
using the progressive notation of big O, the time complexity of Func1 is: O(n²)

N = 10 F(N) = 100
N = 100 F(N) = 10000
N = 1000 F(N) = 1000000
Through the above we will find that the progressive representation of big O removes those items that have little effect on the result, and is concise It clearly shows the number of executions.
In addition, the time complexity of some algorithms has best, average and worst cases:
Worst case: Maximum number of runs (upper bound) of
any input scale Average case: Expected number of runs of
any input scale Best case: Any input scale Minimum number of runs (lower bound)
For example: search for a data x in an array of length N.
Best case: 1 time found.
Worst case: N times found.
Average case: N/2 times found

3. Space complexity

Official concept: It is straightforward for the brightness of an algorithm to temporarily occupy the storage space during its operation
: the space complexity does not depend on how much B of memory the program occupies, but directly depends on the number of variables, and it is the number of variables created in the algorithm. number

Concrete examples

// 计算BubbleSort的空间复杂度?
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}
 }

A constant number of extra spaces is used, so the space complexity is O(1)
If the temporary space required for the execution of the algorithm does not change with the size of a certain variable n, the space complexity is a constant! , Can be expressed as O(1)

// 计算阶乘递归Factorial的空间复杂度?
long long Factorial(size_t N) {
    
    
 return N < 2 ? N : Factorial(N-1)*N; }

Recursively called N times, opened up N stack frames, each stack frame uses a constant space, the space complexity is O(n)

Guess you like

Origin blog.csdn.net/qq_45657288/article/details/109061721