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 }}}}