"Data structure_initial" time and space complexity

Before understanding the time complexity and space complexity, we must first understand the algorithm efficiency. Algorithm efficiency is divided into two types. One is time efficiency, also known as time complexity, and the other is space efficiency, also known as space complexity.
Obviously, time complexity measures how much time an algorithm takes, that is, its running speed, and space complexity measures the space required by the algorithm. Both of these two types of complexity are represented by the progressive representation of Big O. With the development of computers, the storage capacity of computers has reached a very high level. Nowadays, the time complexity of an algorithm is basically considered, and space complexity is no longer a problem that requires special attention.
I made a mind map summarized in this section↓
Insert picture description here
time complexity
calculation time complexity is mainly to calculate the number of executions of the basic operations in the algorithm, using the progressive notation of big O, take the following code as an example to calculate its time complexity

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",count);
}

1. First calculate how many times the basic operation of Func1 has been performed
F(N)=N^2+2*N+10

  • N=10 F(N)=130
  • N=100 F(N)=10210
  • N=1000 F(N)=1002010

2. Use the progressive representation of
Big O: Big O notation, the mathematical notation used to describe the progressive behavior of a function.
The rules of Big O progressive representation are

  • Replace all additive constant terms in the running time with a constant 1
  • Only the highest order items are retained in the modified running count function
  • If the constant of the highest order term is not 1, then remove this constant

According to the rules, the time complexity of the result Func1 is O(N^2)

Space complexity The
main memory of space complexity is to calculate the number of variables rather than how much space the program occupies, so I won’t cite examples.

The difference between the two

  • Time complexity is cumulative calculation
  • Space complexity is reuse

Guess you like

Origin blog.csdn.net/NanlinW/article/details/97787344