1. What is the insertion sort:
Divides the column to be sorted into two parts: ordered data and the element to be inserted.
The basic operation of inserting a sort is to insert the data into a set of ordered data, compare the number to be inserted with each data, and when it finds the first element that is larger than the number of inserts, the element is inserted.
1 namespaceConsoleApplication12 {3 class Program4 {5 Static voidBubble_sort (int[] unsorted)6 {7 //this layer for loop, determines the receive variable (last one does not participate)8 for(inti =1; I < unsorted. Length; i++)9 {Ten intInsert=unsorted[i];//declare a variable that holds the inserted value One intj=0;//declare a variable that records the subscript (you can get the starting point of a bit backward to the subscript element) A while(J < i && insert >Unsorted[j]) - { -J + +; the } - //use a For loop for a post-move operation - for(; J < i; i--) - { +unsorted[i]=unsorted[i-1]; - } + //to insert an interchange AUNSORTED[J] =Insert; at } - } - - Static voidMain (string[] args) - { - int[] x = {6,2,4,1,5,9,3,7};//declares an array of integers and assigns values in //array before sorting -Console.Write ("before sorting:"); to foreach(varIteminchx) + { -Console.Write (item+" "); the } *Bubble_sort (x);//call the bubbling Sort method $ //array after sortingPanax NotoginsengConsole.Write ("\ n after sorting:"); - foreach(varIteminchx) the { +Console.Write (item+" "); A } the console.readline (); + } - } $}
Array--insert Sort method