## 1. Data structure

**data**: the operating object of a program, used to describe objective things. Data includes not only numeric types such as integer and real, but also non numerical types such as character, sound, image and video.*Data has two characteristics*

- It can be input into the computer.
- It can be processed by computer.

**Data element:**The basic unit that makes up data. For example, in humans, people are data elements, while in livestock, cattle, sheep, horses and chickens are data elements.**Data item:**A data element can be composed of several data items. For example, people’s name, gender and age are all data items.*Data item is the smallest unit of data that cannot be separated.***Data object:**It is a collection of data elements with the same property and a subset of data. Similar to arrays.**Data structure:**It is a collection of data elements that have one or more specific relationships with each other. It can also be understood as the relationship between data elements in data objects.

```
//Declare a struct type
Struct person {// a data structure
Char * name; // data item -- name
Char * title; // data item -- Title
Int age; // data item -- age
};
int main(int argc, const char * argv[]) {
Struct person person; // data element;
Struct person PArray [10]; // data object;
person.age =18; // data item
person.name ="Daneil"; // data item
person.title ="Developer"; // data item
Printf ("Name:, person.name );
Printf ("age:, person.age );
Printf ("title of Title: S / N)", person.title );
return 0;
}
```

## 2. Logical structure and physical structure

### 2.1 logical structure

Logical structure: refers to the relationship between data elements in data objects. The logical structure is divided into the following four types:**1. Set structure**

Set structure: data elements in a collection have no relationship with each other except that they belong to the same collection. As shown in the figure below:**2. Linear structure**

Linear structure: there is a one-to-one relationship between data elements in a linear structure. As shown in the figure below:**3. Tree structure**

Tree structure: there is a one to many hierarchical relationship between data elements in tree structure. As shown in the figure below:**4. Graphic structure**

Graphic structure: the data elements of graphic structure are many to many relations. As shown in the figure below:

### 2.2 physical structure

Physical structure refers to the storage form of logical structure of data in computer. There are two storage forms of data elements: sequential storage and chain storage.**1. Sequential storage structure**

Sequential storage structure is to store data elements in a storage unit with continuous addresses, and the logical and physical relationships between data elements are consistent. As shown in the figure below:**2. Chain storage structure**

Chain storage structure is to store data elements in any storage unit, which can be continuous or discontinuous. As shown in the figure below:

The logical structure is problem oriented, while the physical structure is computer oriented. Its basic goal is to store the data and its logical relationship in the computer memory.

## 3. Algorithm

### 3.1 definition of algorithm

An algorithm is a description of the steps to solve a specific problem. It is represented as a finite sequence of instructions in a computer, and each instruction represents one or more operations. Generally speaking, algorithm is to describe the method to solve the problem.

### 3.2 characteristics of the algorithm

**1. Input and output**: the algorithm has zero or more inputs and at least one or more outputs.**2. Poverty**: the algorithm will automatically end without infinite loop after executing finite steps, and each step will be completed in an acceptable time.**3. Certainty**Each step of the algorithm has definite meaning, and there will be no ambiguity.**4. Feasibility**Each step of the algorithm must be feasible, and each step can be completed by executing a limited number of times.

### 3.3 design requirements of algorithm

**1. Correctness**At least, the algorithm should have no ambiguity in input, output and processing, can correctly reflect the needs of the problem, and can get the correct answer to the problem.**2. Readability**Another purpose of algorithm design is to facilitate reading, understanding and communication.**3. Robustness**When the input data is illegal, the algorithm can also make corresponding processing, rather than produce abnormal or inexplicable results.**4. High time efficiency and low storage capacity**The design algorithm should meet the needs of high time efficiency and low storage.

### 3.4 time complexity of the algorithm

#### 3.4.1 large O-order representation

Rule of big O representation:

- Replace all constants in runtime with constant 1. 3->1 O(1)
- In the modified run times function, only the highest order term is retained. n³+2n+5 -> O(n³)
- If the highest order exists and is not equal to 1, the constant 2n? -? N? Multiplied by this term is removed

#### 3.4.2 complexity of common algorithms

**1. Constant order**

```
/*1. Constant order time complexity calculation o (1)*/
//1+1+1 = 3 O(1)
void testSum1(int n){
Int sum = 0; // execute once
Sum = (1 + n) * n / 2; // execute once
Printf ("testsum1: D / N", sum); // execute once
}
//1+1+1+1+1+1+1 = 7 O(1)
void testSum2(int n){
Int sum = 0; // execute once
Sum = (1 + n) * n / 2; // execute once
Sum = (1 + n) * n / 2; // execute once
Sum = (1 + n) * n / 2; // execute once
Sum = (1 + n) * n / 2; // execute once
Sum = (1 + n) * n / 2; // execute once
Printf ("testsum2:
}
//X = x + 1; execute once
void add(int x){
x = x+1;
}
```

**2. Linear order**

```
//X = x + 1; execute n times o (n)
void add(int x,int n){
for (int i = 0; i < n; i++) {
x = x+1;
}
}
//1+(n+1)+n+1 = 3+2n -> O(n)
void testSum3(int n){
Int i, sum = 0; // execute once
For (I = 1; I < = n; I + +) {// execute n + 1 times
Sum + = I; // execute n times
}
Printf ("testsum3:
}
```

**3. Logarithmic order**

```
/*2 to the power of x equals n x = log2n - > O (logn)*/
void testA(int n){
Int count = 1; // execute once
//n = 10
while (count < n) {
count = count * 2;
}
}
```

**4. Square order**

```
//X = x + 1; execute n * n times - > O (n ^ 2)
void add(int x,int n){
for (int i = 0; i< n; i++) {
for (int j = 0; j < n ; j++) {
x=x+1;
}
}
}
```

**5. Cubic order**

```
void testB(int n){
Int sum = 1; // execute once
For (int i = 0; I < n; I + +) {// execute n times
For (int j = 0; J < n; j + +) {// execute n * n times
For (int k = 0; K < n; K + +) {// execute n * n * n times
Sum = sum * 2; // execute n * n * n times
}
}
}
}
```

The time consumed by the common time complexity from small to large is as follows:

### 3.5 worst case and average

The worst-case runtime is a guarantee that it will not go bad again. Usually, unless otherwise specified, we are referring to the worst-case runtime.

The average run time is the most significant of all because it is the expected run time.

### 3.6 spatial complexity of the algorithm

The space complexity of the algorithm is realized by calculating the storage space required by the algorithm. The formula for calculating the space complexity of the algorithm is as follows: s (n) = n (f (n)), where n is the scale of the problem and f (n) is the function of the statement about the storage space occupied by n.**Program space calculation factor:**

- Deposit your own instructions
- constant
- variable
- input
- Auxiliary space for data operation

When considering the space complexity of the algorithm, the auxiliary space needed by the algorithm is mainly considered.

```
//Problem: reverse array order, store the N numbers in one-dimensional array a in the original array`
int main(int argc, const char * argv[]) {
int n = 10;
int a[10] = {1,2,3,4,5,6,7,8,9,10};
//Algorithm implementation (1)
int temp;
for(int i = 0; i < n/2 ; i++){
temp = a[i];
a[i] = a[n-i-1];
a[n-i-1] = temp;
}
//Algorithm implementation (2)
int b[10] = {0};
for(int i = 0; i < n;i++){
b[i] = a[n-i-1];
}
for(int i = 0; i < n; i++){
a[i] = b[i];
}
return 0;
}
```

Obviously, the space complexity of algorithm implementation (2) is larger than that of algorithm implementation (1).

## 4. Summary