A direct insert sort is the simplest sort of insertion .
Insert Sort : Each trip adds a sorted record to the appropriate location of the ordered queue according to the size of its keyword, knowing that all insertions are complete.
(1) We first treat the element labeled 0 in this sequence as an ordered sequence with a number of 1 elements.
(2) Then we have to put R1, R2, ..., RN-1 into this ordered sequence in turn. So, we need an external loop that scans from subscript 1 to N-1.
(3) Next describe the insertion process. Suppose this is to insert an Ri into an ordered sequence in front of you. As mentioned earlier, we know that when you insert an RI, the number of first i-1 is definitely already in order.
So we need to compare the Ri and R0 ~ Ri-1 to determine the appropriate location to insert. This requires an internal loop , which we typically compare back and forth, starting with the subscript i-1 to scan 0.
Basic idea:
Insert a record into the sorted ordered table to get a new, sequential table with a 1 increase in the number of records. That is, the 1th record of the sequence is considered an ordered subsequence, and then inserted from the 2nd record one after the other until the entire sequence is ordered.
Important: Set up Sentinel as a temporary storage and judgment array boundary.
Example of a direct insert sort:
If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.
Core code
Publicvoid Insertsort (Int[] list) {
//The 1th number must be orderly, traversing from the 2nd number, inserting an ordered sequence sequentially
for (int i = 1; i < list.length; i++) {
int j = 0;
int temp = list[i]; // Remove the number of I, compared with the number of previous i-1, insert the appropriate position
// because the number of previous i-1 is an ordered sequence from small to large, so long as the number of current comparisons (List[j]) Larger than temp, move this number back one
for (j = i - 1; j >= 0 && temp < list[j]; j--) {
list[j + 1] = list[j];
}
list[j + 1] = temp;
}
}
Algorithm Analysis
Algorithm performance for direct insert sorting
| Sort category |
Sorting methods |
Complexity of Time |
Complexity of space |
Stability |
Complexity |
| Average situation |
Worst case scenario |
Best case |
| Insert Sort |
Direct Insert Sort |
O (N2) |
O (N2) |
O (N) |
O (1) |
Stability |
Simple |
Complexity of Time
When the data is in a positive order , the execution is best , and each insertion does not move the preceding element, and the time complexity is O (N).
When the data is reversed , the execution is the least efficient, and each insertion is preceded by an element that moves forward, with a time complexity of O (N2).
Therefore, the closer the data is to the positive order, the better the performance of the algorithm for inserting the sort directly .
Complexity of space
By the direct insertion sorting algorithm, we need a temporary variable to store the value to be inserted during the sorting process, so the spatial complexity is 1 .
Algorithm stability
In the process of directly inserting the sort, it is not necessary to change the position of the equal numeric elements, so it is a stable algorithm.
Full Reference Code
Java version
Code Implementation1Import Java.util.Random;
2
3PublicClass Insertsort {
4
5Publicvoid Insertsort (Int[] list) {
6//Print the first element
7System.out.format ("i =%d:\t", 0);
8 Printpart (list, 0, 0);
9
10//The 1th number must be orderly, traversing from the 2nd number, inserting an ordered sequence sequentially
11for (int i = 1; i < list.length; i++) {
12int j = 0;
13int temp = List[i];//Remove the number of I, compared with the number of previous i-1, insert the appropriate position
14
15//Since the first i-1 number is ordered from small to large, so long as the number of current comparisons (List[j]) is larger than the temp, move the number back one
16 for (j = i-1; J >= 0 && temp < list[j]; j--) {
List[j + 1] = List[j];
18}
List[j + 1] = temp;
20
System.out.format ("i =%d:\t", i);
Printpart (list, 0, i);
23}
24}
25
26//Print sequence
27Publicvoid Printpart (Int[] list,int begin,int end) {
28for (int i = 0; i < begin; i++) {
System.out.print ("\ t");
30}
31for (int i = BEGIN; I <= end; i++) {
System.out.print (List[i] + "\ t");
33}
System.out.println ();
35}
36
37PublicStaticvoid Main (string[] args) {
38//Initialize a random sequence
39 Finalint max_size = 10;
40int[] Array =NewInt[max_size];
Random random =New Random ();
42for (int i = 0; i < max_size; i++) {
Array[i] = Random.nextint (max_size);
44}
45
46//Call the Bubbling Sort method
47 insertsort insert = new insertsort ();
48 system.out.print ("before sorting: \ t ");
49 insert.printpart ( ARRAY,&NBSP;0,&NBSP;ARRAY.LENGTH&NBSP;-&NBSP;1);
50 insert.insertsort ( Array);
51 system.out.print (" After sorting: \ t ");
52 insert.printpart ( ARRAY,&NBSP;0,&NBSP;ARRAY.LENGTH&NBSP;-&NBSP;1);
53&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
54&NBSP;} the Java Code implementation of the direct insert sort
Run ResultsBefore sorting: 6 3 3 5 6 3 1 0 6 4
i = 0:6
i = 1:3 6
i = 2:3 3 6
i = 3:3 3 5 6
i = 4:3 3 5 6 6
i = 5:3 3 3 5 6 6
i = 6:1 3 3 3 5 6 6
i = 7:0 1 3 3 3 5 6 6
i = 8:0 1 3 3 3 5 6 6 6
i = 9:0 1 3 3 3 4 5 6 6 6
After sorting: 0 1 3 3 3 4 5 6 6 6
The role of Sentinel
Additional records introduced in the algorithm r[0] are called Sentinel or Sentinel. Sentinel has two functions: ① (insert position) loop before it saves a copy of R[i] so that it does not lose the contents of R[i] due to the post-movement of the record; ② its main function is to "monitor" the subscript variable J in the lookup loop. Once out of bounds (i.e. j=0), because r[0]. Can compare with oneself, the loop judgment condition is not established makes the search loop end, thus avoids in this cycle each time must detect whether J crosses the boundary (namely omitted the cyclic determination condition "j>=1"). Note: ① in fact, all additional nodes (elements) that are introduced to simplify boundary conditions can be called Sentinels.
Direct Insert Sort