Classic sorting algorithm-insert sorting insertion sort
Insert sorting means that each step inserts a data to be sorted to an appropriate position in the sorted data according to its size until all data is inserted.
The insert sorting method is divided into two types: Direct insertion sorting and semi-insertion sorting. Here we only introduce direct insertion sorting, and semi-insertion sorting is left in the "Search" content.
Figure 1 demonstrates the process of directly inserting and sorting the four elements. Three inserts are required: (a), (B), and (c.
The following code is for reference only.
/// <Summary> /// insert sorting /// </Summary> /// <Param name = "unsorted"> </param> static void insertion_sort (INT [] unsorted) {for (INT I = 1; I <unsorted. length; I ++) {If (unsorted [I-1]> unsorted [I]) {int temp = unsorted [I]; Int J = I; while (j> 0 & unsorted [J-1]> temp) {unsorted [J] = unsorted [J-1]; j --;} unsorted [J] = temp ;}} static void main (string [] ARGs) {int [] x = {6, 2, 4, 1, 5, 9 }; insertion_sort (x); foreach (VAR item in X) {If (item> 0) console. writeline (item + ",");} console. readline ();}
Back to main directory [classic Sorting Algorithm] [Collection]