Instance 365 (15) -------------- classic sort ---- insert sort

Source: Internet
Author: User

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

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.