Rust code is slightly different from other languages in the process of writing because of its compiler**No unsafe writing is allowed**Therefore, the longest time spent in the process of code writing is to find the cause of compilation error. It’s also good – after the code is written,**stability**A lot of money!

The following is the ranking definition and action diagram from rookie tutorial:

Insertion sort is a simple and intuitive sorting algorithm. Its working principle is to scan the unordered data from the back to the front in the sorted sequence to find the corresponding position and insert it.

The main logic of insertion sort is as follows:

- The outer loop first specifies a number, usually the first one
- Then compare the specified number of the outer loop with the number on the left side one by one in the inner loop, and insert the specified number of the outer loop into the left or fixed number of the inner loop according to the comparison result
- The left side is the sorted element, so the comparison in the inner loop is that the specified number in the outer loop is constantly compared with the element on the left side
- When the specified number of outer loop is less than the left element, the position will be exchanged, otherwise it will not be changed
- And so on, until the end of the outer loop

Now there is a set of elements to sort:

`[7, 21, 9, 13, 109, 9, 2, 50, 33, -1, 20, 11]`

Specifies the first number by the logical outer loop of the insert sort**7**When selected, the for loop starts with the element with a subscript of 1. The code is as follows:

`for i in 1..vectors.len(){}`

The subscript is**0**The first number of**7**If the for loop is sorted, the subscript is**1**The second number of**21**Here we go. In the inner loop, compare the specified number of the outer loop with the left number [7] one by one. When the specified number of the outer loop is less than the left element, exchange the position, otherwise it will not move.

Enter at this time**The next round of external circulation**, specify the subscript as**2**The third number of**9**。 In the inner loop, the specified number of the outer loop is compared with the left number [7, 21] one by one, when the specified number of the outer loop is less than the left element**21**Exchange location when, encounter**7**Don’t move. Then the element group is changed into:

[7, **9**, **21**, 13, 109, 9, 2, 50, 33, -1, 20, 11]

Enter at this time**The next round of external circulation**, specify the subscript as**3**The fourth number of**13**。 In the inner loop, the specified number of the outer loop is compared with the left number [7, 9, 21] one by one, when the specified number of the outer loop is less than the left element**21**Exchange location when, encounter**9**Don’t move. Then the element group is changed into:

[7, 9, **13**, **21**, 109, 9, 2, 50, 33, -1, 20, 11]

And so on

Enter at this time**The nth round of external circulation**, specify the subscript as**5**The sixth number of**9**。 In the inner loop, compare the number specified in the outer loop with the number on the left [7, 9, 13, 21, 109] one by one

- When the specified number of outer loop is less than the left element
**109**The position is exchanged in time; - When the specified number of outer loop is less than the left element
**21**The position is exchanged in time; - When the specified number of outer loop is less than the left element
**13**The position is exchanged in time;

encounter**9**Don’t move. Then the element group is changed into:

[7, 9, 9, **13**, **21**, **109**, 2, 50, 33, -1, 20, 11]

Enter at this time**The nth round of external circulation**, specify the subscript as**6**The seventh number of**2**。 In the inner loop, compare the number specified in the outer loop with the number on the left [7, 9, 9, 13, 21, 109] one by one

- When the specified number of outer loop is less than the left element
**109**The position is exchanged in time; - When the specified number of outer loop is less than the left element
**21**The position is exchanged in time; - When the specified number of outer loop is less than the left element
**13**The position is exchanged in time; - When the specified number of outer loop is less than the left element
**9**The position is exchanged in time; - When the specified number of outer loop is less than the left element
**9**The position is exchanged in time; - When the specified number of outer loop is less than the left element
**7**The position is exchanged in time;

Traverse to the left end of the element group, at this time j is not greater than or equal to 0, the end of the inner loop. Then the element group is changed into:

[2, **7**, **9**, **9**, **13**, **21**, **109**, 50, 33, -1, 20, 11]

Before that, there were many elements on the left side of 2, and it needed to compare them one by one and swap positions. The position change process of number 2 is as follows:

```
[7, 9, 9, 13, 21, 2, 109, 50, 33, -1, 20, 11]
[7, 9, 9, 13, 2, 21, 109, 50, 33, -1, 20, 11]
[7, 9, 9, 2, 13, 21, 109, 50, 33, -1, 20, 11]
[7, 9, 2, 9, 13, 21, 109, 50, 33, -1, 20, 11]
[7, 2, 9, 9, 13, 21, 109, 50, 33, -1, 20, 11]
[2, 7, 9, 9, 13, 21, 109, 50, 33, -1, 20, 11]
```

It can be seen from the process that the number 2 will move to the left in each round of the inner circle until there is no number larger than 2 in front of it.

By analogy, the final result of element sorting is as follows:

`[-1, 2, 7, 9, 9, 11, 13, 20, 21, 33, 50, 109]`

## Specific code implementation

First, define a set of elements and print:

```
fn main() {
let mut vectors = vec![7, 21, 9, 13, 109, 9, 2, 50, 33, -1, 20, 11];
println!("vectors: {:?}", vectors);
}
```

Then define the sorting method:

```
fn insert_sort(vectors: &mut Vec<i32>) -> &Vec<i32>{
vectors
}
```

The outer loop of sorting method is for loop

```
fn insert_sort(vectors: &mut Vec<i32>) -> &Vec<i32>{
for i in 1..vectors.len(){
}
vectors
}
```

Here, the outer element is assigned to the variable current, and the subscript of the element on the left side of the inner loop is set

```
fn insert_sort(vectors: &mut Vec<i32>) -> &Vec<i32>{
for i in 1..vectors.len(){
let mut current = vectors[i];
let mut j = i - 1;
}
vectors
}
```

In the inner loop, the specified number of the outer loop is compared with the number on the left side one by one. When the specified number of the outer loop is less than the element on the left side, the position is exchanged; otherwise, it does not move.

Compared with the element on the left, j = I – 1, vectors [J] is used;

Constantly comparing with the left element, it is represented by while J > = 0, j = J – 1;

The comparison is represented by current < vectors [J];

The swap position can’t be a, B = B, a like python, only C = a, a = B, B = C can be used to add the third number. The code is as follows

```
fn insert_sort(vectors: &mut Vec<i32>) -> &Vec<i32>{
for i in 1..vectors.len(){
let mut current = vectors[i];
let mut j = i - 1;
while j >= 0 && current < vectors[j]{
let middle = vectors[j+1];
vectors[j+1] = vectors[j];
vectors[j] = middle;
j = j - 1;
}
}
vectors
}
```

However, if it is written in this way, the specified number of the outer loop is smaller than all the numbers on the left side, it will not pass the compilation. For example, when the number specified in the outer loop is 2, it needs to be compared with all the numbers on the left until J = 0, but the last sentence in while is j = J – 1. After running here, j = – 1. According to the normal operation process, the program will enter the next round of inner loop with while J > = 0, but because J = – 1, it will not enter the body of the while loop. Python language is no problem to write like this, but rust’s compiler does not allow it, so we need to add the control statement if J = 0 in the outer layer of J = J – 1.

## Verification of the theory

The above theory seems reasonable and convincing, but is it right?

Is it possible to analyze errors?

Although the program is right, what if the logic described is wrong?

We can observe the change of element position during loop comparison by printing the specified number of outer loop current, the first number on the left side of inner loop and the element group vectors after each position exchange. The code to add the print statement is as follows:

```
fn insert_sort(vectors: &mut Vec<i32>) -> &Vec<i32>{
for i in 1..vectors.len(){
let mut current = vectors[i];
let mut j = i - 1;
println!("current vectors: {:?}", vectors);
println!("current: {} < vectors[j]: {}", current, vectors[j]);
while j >= 0 && current < vectors[j]{
let middle = vectors[j+1];
vectors[j+1] = vectors[j];
vectors[j] = middle;
if j > 0{
/*While J > = 0 is not allowed to subtract 1 when J = 0 in trust
The dangerous way of writing that causes J to be negative in while*/
j = j - 1; // J decreasing means constantly comparing with the left
}
println!("after vectors: {:?}", vectors);
}
}
vectors
}
```

The print result after code running is as follows:

```
vectors: [7, 21, 9, 13, 109, 9, 2, 50, 33, -1, 20, 11]
current vectors: [7, 21, 9, 13, 109, 9, 2, 50, 33, -1, 20, 11]
current: 21 < vectors[j]: 7
current vectors: [7, 21, 9, 13, 109, 9, 2, 50, 33, -1, 20, 11]
current: 9 < vectors[j]: 21
after vectors: [7, 9, 21, 13, 109, 9, 2, 50, 33, -1, 20, 11]
current vectors: [7, 9, 21, 13, 109, 9, 2, 50, 33, -1, 20, 11]
current: 13 < vectors[j]: 21
after vectors: [7, 9, 13, 21, 109, 9, 2, 50, 33, -1, 20, 11]
current vectors: [7, 9, 13, 21, 109, 9, 2, 50, 33, -1, 20, 11]
current: 109 < vectors[j]: 21
current vectors: [7, 9, 13, 21, 109, 9, 2, 50, 33, -1, 20, 11]
current: 9 < vectors[j]: 109
after vectors: [7, 9, 13, 21, 9, 109, 2, 50, 33, -1, 20, 11]
after vectors: [7, 9, 13, 9, 21, 109, 2, 50, 33, -1, 20, 11]
after vectors: [7, 9, 9, 13, 21, 109, 2, 50, 33, -1, 20, 11]
current vectors: [7, 9, 9, 13, 21, 109, 2, 50, 33, -1, 20, 11]
current: 2 < vectors[j]: 109
after vectors: [7, 9, 9, 13, 21, 2, 109, 50, 33, -1, 20, 11]
after vectors: [7, 9, 9, 13, 2, 21, 109, 50, 33, -1, 20, 11]
after vectors: [7, 9, 9, 2, 13, 21, 109, 50, 33, -1, 20, 11]
after vectors: [7, 9, 2, 9, 13, 21, 109, 50, 33, -1, 20, 11]
after vectors: [7, 2, 9, 9, 13, 21, 109, 50, 33, -1, 20, 11]
after vectors: [2, 7, 9, 9, 13, 21, 109, 50, 33, -1, 20, 11]
current vectors: [2, 7, 9, 9, 13, 21, 109, 50, 33, -1, 20, 11]
current: 50 < vectors[j]: 109
after vectors: [2, 7, 9, 9, 13, 21, 50, 109, 33, -1, 20, 11]
current vectors: [2, 7, 9, 9, 13, 21, 50, 109, 33, -1, 20, 11]
current: 33 < vectors[j]: 109
after vectors: [2, 7, 9, 9, 13, 21, 50, 33, 109, -1, 20, 11]
after vectors: [2, 7, 9, 9, 13, 21, 33, 50, 109, -1, 20, 11]
current vectors: [2, 7, 9, 9, 13, 21, 33, 50, 109, -1, 20, 11]
current: -1 < vectors[j]: 109
after vectors: [2, 7, 9, 9, 13, 21, 33, 50, -1, 109, 20, 11]
after vectors: [2, 7, 9, 9, 13, 21, 33, -1, 50, 109, 20, 11]
after vectors: [2, 7, 9, 9, 13, 21, -1, 33, 50, 109, 20, 11]
after vectors: [2, 7, 9, 9, 13, -1, 21, 33, 50, 109, 20, 11]
after vectors: [2, 7, 9, 9, -1, 13, 21, 33, 50, 109, 20, 11]
after vectors: [2, 7, 9, -1, 9, 13, 21, 33, 50, 109, 20, 11]
after vectors: [2, 7, -1, 9, 9, 13, 21, 33, 50, 109, 20, 11]
after vectors: [2, -1, 7, 9, 9, 13, 21, 33, 50, 109, 20, 11]
after vectors: [-1, 2, 7, 9, 9, 13, 21, 33, 50, 109, 20, 11]
current vectors: [-1, 2, 7, 9, 9, 13, 21, 33, 50, 109, 20, 11]
current: 20 < vectors[j]: 109
after vectors: [-1, 2, 7, 9, 9, 13, 21, 33, 50, 20, 109, 11]
after vectors: [-1, 2, 7, 9, 9, 13, 21, 33, 20, 50, 109, 11]
after vectors: [-1, 2, 7, 9, 9, 13, 21, 20, 33, 50, 109, 11]
after vectors: [-1, 2, 7, 9, 9, 13, 20, 21, 33, 50, 109, 11]
current vectors: [-1, 2, 7, 9, 9, 13, 20, 21, 33, 50, 109, 11]
current: 11 < vectors[j]: 109
after vectors: [-1, 2, 7, 9, 9, 13, 20, 21, 33, 50, 11, 109]
after vectors: [-1, 2, 7, 9, 9, 13, 20, 21, 33, 11, 50, 109]
after vectors: [-1, 2, 7, 9, 9, 13, 20, 21, 11, 33, 50, 109]
after vectors: [-1, 2, 7, 9, 9, 13, 20, 11, 21, 33, 50, 109]
after vectors: [-1, 2, 7, 9, 9, 13, 11, 20, 21, 33, 50, 109]
after vectors: [-1, 2, 7, 9, 9, 11, 13, 20, 21, 33, 50, 109]
results: [-1, 2, 7, 9, 9, 11, 13, 20, 21, 33, 50, 109]
```

Thus, the description of the theoretical part is correct

The complete rust insert sort code is as follows:

Code warehouse address of rust algorithmhttps://github.com/asyncins/a…