This code design is concise and may not be easy to understand. insertion 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 inserts are completed. I:
Ii. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace InsertSort {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private int [] G_int_value; // define the array FIELD private Random G_Random = new Random (); // create a Random number object private void btn_sort_Click (object Sender, EventArgs e) {if (G_int_value! = Null) {for (int I = 0; I <G_int_value.Length; ++ I) // elements in the loop access Array {int temp = G_int_value [I]; // define an int variable and assign an int j = I using the obtained array element value. while (j> 0) & (G_int_value [j-1]> temp )) // determine whether the element in the array is greater than the obtained value {G_int_value [j] = G_int_value [j-1]; // If yes, the value of the last element is advanced -- j;} G_int_value [j] = temp; // Finally, the value stored in the int variable is assigned to the last element} txt_str2.Clear (); // clear the control string foreach (int I in G_int_value) // traverses the string set {txt_str2.Text + = I. toSt Ring () + ","; // Add a string to the control} else {MessageBox. Show ("the array should be generated first and then sorted. "," Prompt! ") ;}} Private void btn_Generate_Click (object sender, EventArgs e) {G_int_value = new int [G_Random.Next (10, 20)]; // generate a random length array for (int I = 0; I <G_int_value.Length; I ++) // traverse the Array {G_int_value [I] = G_Random.Next (0,100 ); // assign a random value to the array} txt_str.Clear (); // clear the control string foreach (int I in G_int_value) // traverse the string set {txt_str.Text + = I. toString () + ","; // Add a string to the control }}}}