Tag:pre thread Array reading gen thought name log time
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace Insert Sort {class Program {static void Main (string[] args) {int[] Arry = {10,3,4,6,7 , 9,8,2,1,0}; Insertsortpro (Arry, 10); for (int i=0;i<10;i++) {Console.WriteLine (arry[i]); } console.readkey (); }///<summary>///Insert Sort//thought: The second element is compared to the first element, and if it is less than the first element, the position is placed at the first element.//The third element and the first two elements are compared, less than the previous The two elements are placed in the corresponding position///etc..//</summary>//<param name= "Arry" ></param>// <param name= "Length" ></param> public static void Insertsort (int[] arry,int length) { for (int i =1;i<length;i++)//First element does not need to compare {for (int j=i;j>0;j--) { if (Arry[j-1]>arry[j]) {Swap (Arry, J-1, J); } else {break; }}}///<summary>///Insert Sort Introduction///</summary> <param name= "Arry" ></param>///<param name= "Length" ></param> public static VO ID InsertSort2 (int[] arry, int length) {for (int i = 1; i < length; i++)//The first element does not require a comparison { for (int j = i; j > 0 && arry[j-1] > Arry[j]; j--) {Swap (a Rry, J-1, J); }}}///<summary>/////Optimized Insert sort///In the previous insert sort, the inner loop is to be exchanged for elements, which is an important reason for the time to insert sort consumption Below we optimize the insertion sort, "Only compare not Exchange"//</summary>//<param name= "Arry" ></param>// <param name= "Length" ></param> public static void Insertsortpro (int[] arry,int length) {for (int i=1;i<length;i++) {int e = arry[i];//Get the current element, see if this element is the smallest number in the first I element int J;//position of current element for (j=i;j>0&&arry[j-1]>e;j--)//determine e This element is not the smallest number in the first I element {arry[j]= arry[j-1];//A previous element to move backward one} Arry[j] = e; }} private static void Swap (int[] arr, int i, int j) {int t = Arr[i]; Arr[i] = Arr[j]; ARR[J] = t; } }}
"Normal insert Sort" is slower than selecting sort, but optimized insert ordering is faster than selecting sort. Insert sort has a feature when the array is more orderly, the sort will be faster. Sometimes it's faster than some advanced sort.
Also the time complexity of inserting sort is also O (n^2)
Insertion sequencing of algorithms and data structures