The interrelationships between data are called logical structures . Generally divided into four basic structures:
Data elements in a collection structure have no other relationship than the same type.
There is a one-to-one relationship between data elements in a linear structure .
There is a one-to-many relationship between the data elements in a tree-structured structure.
There are many-to-many relationships between data elements in a graph structure or a mesh structure.
The data structure has two different storage methods in the computer:
Sequential Storage structure : Represents the logical relationship between data elements in the relative position of data elements in memory.
chained storage structure : Add a pointer to each data element to hold the address, and use this pointer to represent the logical relationship between the data elements.
Complexity of Time
The time spent in an algorithm is proportional to the number of executions of the statement in the algorithm, which takes more time to execute than the number of statements executed in the algorithm. The number of times a statement is executed in an algorithm is called a statement frequency or time frequency . Remember as T (N)
In the time frequency mentioned just now, N is called the scale of the problem, and when N is constantly changing, the time frequency t (n) will change constantly. But sometimes we want to know what the pattern is when it changes. To do this, we introduce the concept of time complexity.
Under normal circumstances, the number of iterations of the basic operation of the algorithm is a function of the problem size n, denoted by T (n), if there is an auxiliary function f (n), so that when n approaches infinity, the limit value of T (n)/f (n) is not equal to zero constant, then f (n) is the same order of magnitude function of t As T (n) =o (f (n)), called O (f (n)) is the progressive time complexity of the algorithm, which is referred to as the complexity of time.
Sometimes, the number of executions of basic operations in the algorithm is different from the input data set of the problem, such as in bubble sort, the input data is ordered and unordered, the result is not the same. At this point, we calculate the average.
The relationship between the time complexity of the common algorithm is:
O (1) <o (LOGN) <o (n) <o (nlog N) <o (n2) <o (2n) <o (n!) <o (NN)
Example 1
sum=0; //(1) for (i=1;i<=n;i++) //(2) for (j=1;j<=n;j++) //(3) sum++; //(4)
Statement (1) executes 1 times,
Statement (2) executes n times
Statement (3) executes N2 times
Statement (4) executes N2 times
T (n) = 1+n+2n2= O (n2)
Example 2
a=0; b=1; (1) for (i=1;i<=n;i++) //(2) { s=a+b; //(3) b=a; //(4) a=s; //(5) }
Statement (1) executes 1 times,
Statement (2) executes n times
Statements (3), (4), (5) Execute n times
T (n) = 1+4n =o (n)
Example 3
I=1; (1) while (i<=n) i=i*2; (2)
The frequency of the statement (1) is 1,
The frequency of setting statement 2 is f (n), then: 2f (N) <=n;f (n) <=log2n
The maximum value f (n) = log2n,
T (n) =o (log2n)
Complexity of space
Spatial complexity: The measurement of the storage space required by the algorithm is recorded as:
S (n) =o (f (n))
where n is the size of the problem.
The storage space required for an algorithm: the storage space of the algorithm itself, the storage space of the input data, and the storage space temporarily occupied by the algorithm during the running process. If the extra space is a constant relative to the amount of input data, the algorithm is said to work in situ.
The storage space occupied by an algorithm in the computer memory, including the storage space occupied by the storage algorithm itself, the storage space occupied by the input and output data of the algorithm and the storage space occupied by the algorithm in the running process three aspects. The storage space occupied by the input and output data of the algorithm is determined by the problem to be solved, which is passed by the calling function by the parameter table, and it does not change with the algorithm. Storage algorithm itself occupies the storage space and the length of the algorithm written in proportion, to compress the storage space, you must write a shorter algorithm.