Data Structure - 1 Basic Concepts

1 Basic Concepts

1.1 What is a data structure

Example 2 passed a positive integer N, the order of printing all positive integer from 1 to N:

//递归实现
void PrintN(int N){
	if(N){
		PrintN(N-1);
		printf("%d\n",N);
	}
}

Recursive program takes up a large space
to solve the efficiency of the method, with the efficiency of space related to
Here Insert Picture Description
the second standard function
Here Insert Picture Description
using clock () to test the program running time

, however, some of the program finish time spent will be very small, approximately equal to 0, then , you can 重复execute the program

What is a data structure

Here Insert Picture Description
Logical Structure: linear structure (one to one), trees (one to many), FIG (many)
physical storage structure: it is used in a computer array or linked list to save memory

Abstract data type

Here Insert Picture Description
Here Insert Picture Description
For a float or double is not care, ElementType is a type of

1.2 What is an algorithm

Here Insert Picture Description

What is a good algorithm

Time complexity T (n)

According to the algorithm written in the program execution time 耗费时间的长度. This length is often associated with the size of the input data. Too high will lead not wait for the results.

Space complexity S (n)

According to the algorithm written in the program execution time 占用存储单元的长度. This length is often associated with the size of the input data. Excessive use can cause memory overrun, causing the program is interrupted abnormally.
Here Insert Picture Description
Recursive each call takes up a certain amount of memory space to store information, but only for a temporary variable cycle and for the cycle, there is no question of any design program called, so he occupied space is a constant.
Here Insert Picture Description
Here Insert Picture Description
Example:
Here Insert Picture Description
Algorithm 1: Violence Act. Three nested for-loop, may prove to be a N . 3 * 1 constants
Here Insert Picture Description
can be seen from the figure, when the sum of i to j know, together with a number on it, so that the cycle k may be omitted
Here Insert Picture Description
to the extent possible of the n- 2 improved to nlogN

Algorithm 3: divide and conquer, to a large complex problems cut into small pieces, and then split up to solve them, and finally the results are combined to
Here Insert Picture Descriptiondivide it into two parts, the left half of the recursive search of the biggest, right partial recursive lookup largest in the Find the middle of some of the largest
Here Insert Picture Description
computing time complexity: imagine, when the whole problem solved, there are N numbers of time, if complexity is denoted by T (N), then the availability of the complexity of the half figures to T (N / 2), because of the size halved.
Red intermediate portion: since the intermediate scanning from the left to the right and then scanning, each element 1 are scanned, the complexity should be a constant multiple of N
Here Insert Picture Description
Here Insert Picture Description
correctness is not obvious (not understood)
Here Insert Picture Description
Here Insert Picture Description

Published 16 original articles · won praise 0 · Views 236

Guess you like

Origin blog.csdn.net/qq_42713936/article/details/104679842