data structure
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
the second standard function
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
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
For a float or double is not care, ElementType is a type of
1.2 What is an algorithm
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.
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.
Example:
Algorithm 1: Violence Act. Three nested for-loop, may prove to be a N . 3 * 1 constants
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
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
divide 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
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
correctness is not obvious (not understood)