【Data Structure】First understand the time and space complexity of data structure.

The road is long and long, and I will search up and down. —— Qu Yuan

 

Table of contents

1. What is a data structure?

2. What is an algorithm?

1. The complexity of the algorithm

2. Time complexity

3. Time complexity exercises

4. Space complexity


1. What is a data structure?

A data structure is a collection and manipulation of data elements that have one or more specific relationships with each other.

It refers to the interrelationships between data elements, that is, the organization of data. This organizational form is the logical structure of the data.

The logic of data usually has the following four basic structures:

1. Collective institutions

2. Linear structure

3. Tree structure

4. Graph structure

In the process of actually processing data by the computer, we must consider how the data should be stored so that it can reflect the relationship between the data.

The way data is stored in the computer is the storage structure of the data . For example, the sequential list, linked list, etc. to be learned later .

In addition, in the process of data processing, operations such as data deletion, insertion, and search will also occur , so we should also consider the way of data processing, that is, the algorithm .

2. What is an algorithm?

Algorithm (Algorithm) refers to the accurate and complete description of the problem-solving scheme, which is a series of clear instructions to solve the problem. Algorithm represents the strategy mechanism to describe the problem-solving in a systematic way. . That is to say, the required output can be obtained within a limited time for a certain standard input .

Like bubble sorting, finding a number, checking for duplicates, etc. are all algorithms.

Features of the algorithm:

Finiteness: An algorithm must be able to terminate after executing a finite number of steps.

Exactness: Each step of the algorithm must have an exact definition.

Inputs: An algorithm has 0 or more inputs to describe the initial situation of the operand.

Output item: An algorithm has one or more outputs to reflect the result of processing the input data, and an algorithm without output is meaningless.

Feasibility: Any computational step performed in an algorithm can be broken down into basic executable operational steps.

1. The complexity of the algorithm

After the algorithm is compiled into an executable program, it needs to consume time resources and space (memory) resources when running. Therefore, to measure the quality of an algorithm, it is generally measured from two dimensions of time and space, that is, time complexity and space complexity.

Time complexity mainly measures how fast an algorithm runs, while space complexity mainly measures the extra space required for an algorithm to run. In the early days of computer development, computers had very little storage capacity . So I am very concerned about the space complexity. However, with the rapid development of the computer industry, the storage capacity of computers has reached a very high level. So we no longer need to pay special attention to the space complexity of an algorithm.

2. Time complexity

Definition of time complexity: In computer science, the time complexity of an algorithm is a function (mathematical 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 a computer? Yes, we can test all algorithms on a computer, but this is very troublesome, so we have the analysis method of time complexity. The time spent by an algorithm is proportional to the number of executions of the statements in it , and the number of executions of the basic operations in the algorithm is the time complexity of the algorithm .

Summary: The algorithm is not the amount of running time, because when you are running the code, the speed of the time is also related to your computer configuration and network speed at this time. So just find the number of runs of the algorithm. That is, the mathematical expression between a certain basic statement and the problem size N is to calculate the time complexity of the algorithm.

Example: Calculate how many times the count++ statement in Fun1 has been executed?


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

The total number of loops above: the expression of the mathematical function F(N)=N^2+2*N+10.

The time complexity is F(N)=N^2+2*N+10.

When N takes different values:

  • N = 10          F(N) = 130
  • N = 1000      F(N) = 1002010
  • N = 100000  F(N) = 10000200010

Here we can see that as N gradually increases, 2*N+10 in F(N)=N^2+ 2*N+10 has less and less influence on the entire function value. The time complexity is getting closer and closer to N^2, we can say that N^2 is the time complexity of this algorithm.

For the complexity of time, we use the asymptotic notation of big 0:
big 0 notation (Big 0 notation)  : It is a mathematical symbol used to describe the asymptotic behavior of functions.

Derivation of the Big 0 method: 
1. Replace all additive constants in run time with a constant 1 . 2. In the modified running times function, only the highest order term is kept . 3. If the highest order item exists and is not 1 , remove the constant multiplied by this item . The result obtained is the order of 0.

After using the asymptotic representation of big 0, the time complexity of Func1 is: O(N^2).

Through the above, we will find that the progressive representation of big 0 removes those items that have little influence on the result, and expresses the number of executions concisely and clearly. In addition, there are best, average and worst case time complexities of some algorithms :


Worst case: Maximum number of runs for any input size (upper bound)
Average case: Expected number of runs for any input size
Best case: Minimum number of runs for any input size (lower bound)


For example: Searching for a data x in an array of length N
Best case: 1 find
Worst case: N finds
Average case: N/2 finds
In practice, the general case focuses on the worst operating conditions of the algorithm , So the time complexity of searching data in the array is 0(N) .

3. Time complexity exercises

1. What is the time complexity of Func2?

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

Exact F(N)=2*N+10.

As N continues to increase, the size of F(N) has nothing to do with 10, so when expressing time complexity, 10 must be removed, and according to the third point in the derivation of the large 0-order method: 3. If If the highest order term exists and is not 1 , the constant multiplied by this term is removed . The result obtained is the order of 0. So 2 in 2*N can also be removed.

Therefore, the time complexity of Func2 is O(N).

2. What is the time complexity of calculating Func3?

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);
}

Obviously there is no obvious relationship between M and N, so the time complexity is O(N+M).

1. If N is much larger than M, the time complexity is O(N).

2. If M is much larger than N, the time complexity is O(M).

3. If M and N are about the same size, the time complexity is O(N) or O(M).

3. Calculate the time complexity of strchr?

The function of the strchr function is to find a specified character in a string, and if the character is found, it returns the address where the first character appears. If the character is not found after searching the string, a null pointer is returned.

#include<string.h>//NULL空指针需要的头文件
#include<assert.h>//assert必须要的头文件
char* strchr(const char* str, int character)
{ //字符的ASCll值就是数子,所以这里字符的数据类型可以用int
	assert(str);
	while (*str)
	{
		if (*str == character)
			return str;
		else
			str++;
	}
	return NULL;
 }

 When calculating the time complexity, we all calculate in the worst case , so the time complexity of the strchr algorithm is O(N).

3. Calculate the time complexity of bubble sort?

void Bulllesort(int *arr,int N)
{
    assert(arr);
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N - i; j++)
        {
            if (arr[j] > arr[j + 1]) {
                int ret = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = ret;
            }
        }
    }

 It can be seen here that when entering the bubble sort for the first time, the array needs to be exchanged 9 times, when entering the bubble sort for the second time, the array needs to be exchanged 8 times, and so on. In fact, this is an arithmetic sequence. F(N)=N(1+N)/2, so the time complexity is O(N^2).

4. Calculate the time complexity of binary search?

int Binarysearch(int nums[], int size, int target) 
{
    int left = 0;
    int right = size - 1;	
    while (left <= right) {
        int middle = left + ((right - left) / 2);
        if (nums[middle] > target)
        {
            right = middle - 1;
        }
        else if (nums[middle] < target)
        {
            left = middle + 1;
        }
        else {
            return middle;
        }
        }
}

We calculate the time complexity in the worst case. In binary search, the worst case is that the searched number is exactly in the middle. That is 2^x=N, x is the number of searches. In mathematics, log is logarithm with base 2, but it is not easy to type on the keyboard, so it is simplified to log2, so the time complexity of binary search That's O(log2).

4. Space complexity

Space complexity is also a mathematical function expression, which is a measure of the temporary storage space occupied by an algorithm during operation.
Space complexity is not how many bytes the program occupies , because this is not very meaningful. So the space complexity counts the number of variables . The space complexity calculation rules are basically similar to the practical complexity, and the big 0 asymptotic representation is also used.
Note: 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.
 

void Bulllesort(int *arr,int N)
{
    assert(arr);
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N - i; j++)
        {
            if (arr[j] > arr[j + 1]) {
                int ret = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = ret;
            }
        }
    }

Like this bubble sort, in addition to the parameters in the function, two additional variables i and j are defined , so the space complexity is O(1).

Nowadays, the space capacity of computers is relatively large, and the space complexity is not very important.

Common complexity comparisons:

 

 

It is not easy to create, I hope everyone can give some likes, thank you. 

おすすめ

転載: blog.csdn.net/adcxhw/article/details/129509152