C ා bubble sort program

Posted by xplore on Sat, 14 Dec 2019 15:51:39 +0100

Considering that many interviews may examine the use of bubble sorting, I took the time to clarify my thinking. Let's talk about my thoughts:
The core of bubble sorting is the comparison method. The comparison method of bubble sorting, as the name implies, is like a bubble, with the maximum (or minimum) number rising.
We can use if (a > b) and then c=a; b=a.... In this way, large numbers are temporarily stored in c, and then small numbers are stored in
The original smaller numbers continue to be compared with other numbers. The same is true for bubble sorting. However, there are a lot of data in bubble sorting, so a for loop is needed,
After comparing one number, compare the next one, cycle to the last one, find out the largest number first, then find the second largest one, and so on.
The implementation procedure is as follows:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace BubbleUpSort
11 {
12     public partial class Frm_Main : Form
13     {
14         public Frm_Main()
15         {
16             InitializeComponent();
17         }
18 
19         private int[] G_int_value;//Defining array fields
20 
21         private Random G_Random = new Random();//Create random number object
22 
23         private void btn_sort_Click(object sender, EventArgs e)
24         {
25             if (G_int_value != null)
26             {
27                 //Define two int Type to represent array subscripts and to store new array elements
28                 int j, temp;
29                 for (int i = 0; i < G_int_value.Length - 1; i++)//Traversing array elements based on the value of array subscript
30                 {
31                     j = i + 1;
32                 id://Define an identity to start the statement from here
33                     if (G_int_value[i] > G_int_value[j])//Judge the size of two numbers before and after
34                     {
35                         temp = G_int_value[i];//Assign the large element after comparison to the defined int variable
36                         G_int_value[i] = G_int_value[j];//Assign the value of the next element to the previous element
37                         G_int_value[j] = temp;//take int The value of the element stored in the variable is assigned to the next element
38                         goto id;//Return identification, continue to judge the following elements
39                     }
40                     else
41                         if (j < G_int_value.Length - 1)//Determine whether to execute to the last element
42                         {
43                             j++;//If not, judge later 
44                             goto id;//Return identification, continue to judge the following elements
45                         }
46                 }
47                 txt_str2.Clear();//Empty string in control
48                 foreach (int i in G_int_value)//Traversal string collection
49                 {
50                     txt_str2.Text += i.ToString() + ", ";//Add string to control
51                 }
52             }
53             else
54             {
55                 MessageBox.Show("You should first generate an array and then sort it.", "Tips!");
56             }
57         }
58 
59         private void btn_Generate_Click(object sender, EventArgs e)
60         {
61             G_int_value = new int[G_Random.Next(10, 20)];//Generate random length array
62             for (int i = 0; i < G_int_value.Length; i++)//Traversing array
63             {
64                 G_int_value[i] = G_Random.Next(0, 100);//Assign random values to arrays
65             }
66             txt_str.Clear();//Empty string in control
67             foreach (int i in G_int_value)//Traversal string collection
68             {
69                 txt_str.Text += i.ToString() + ", ";//Add string to control
70 
71             }
72         }
73     }
74 }

The design code is as follows:

namespace BubbleUpSort
{
    partial class Frm_Main
    {
        /// <summary>
        /// Required designer variables.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up all resources in use.
        /// </summary>
        /// <param name="disposing">If managed resources should be released, for true;Otherwise, false. </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Code generated by form designer

        /// <summary>
        /// Methods required for designer support - Do not
        /// Use the code editor to modify the contents of this method.
        /// </summary>
        private void InitializeComponent()
        {
            this.btn_sort = new System.Windows.Forms.Button();
            this.btn_Generate = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.txt_str = new System.Windows.Forms.TextBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.txt_str2 = new System.Windows.Forms.TextBox();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // btn_sort
            // 
            this.btn_sort.Location = new System.Drawing.Point(107, 67);
            this.btn_sort.Name = "btn_sort";
            this.btn_sort.Size = new System.Drawing.Size(86, 23);
            this.btn_sort.TabIndex = 0;
            this.btn_sort.Text = "Bubble sort";
            this.btn_sort.UseVisualStyleBackColor = true;
            this.btn_sort.Click += new System.EventHandler(this.btn_sort_Click);
            // 
            // btn_Generate
            // 
            this.btn_Generate.Location = new System.Drawing.Point(107, 70);
            this.btn_Generate.Name = "btn_Generate";
            this.btn_Generate.Size = new System.Drawing.Size(86, 23);
            this.btn_Generate.TabIndex = 1;
            this.btn_Generate.Text = "Generate random array";
            this.btn_Generate.UseVisualStyleBackColor = true;
            this.btn_Generate.Click += new System.EventHandler(this.btn_Generate_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.txt_str);
            this.groupBox1.Controls.Add(this.btn_Generate);
            this.groupBox1.Location = new System.Drawing.Point(12, 10);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(305, 100);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Generate random array";
            // 
            // txt_str
            // 
            this.txt_str.Location = new System.Drawing.Point(6, 20);
            this.txt_str.Multiline = true;
            this.txt_str.Name = "txt_str";
            this.txt_str.Size = new System.Drawing.Size(293, 44);
            this.txt_str.TabIndex = 4;
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.txt_str2);
            this.groupBox2.Controls.Add(this.btn_sort);
            this.groupBox2.Location = new System.Drawing.Point(12, 116);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(305, 97);
            this.groupBox2.TabIndex = 3;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Sort random array";
            // 
            // txt_str2
            // 
            this.txt_str2.Location = new System.Drawing.Point(6, 20);
            this.txt_str2.Multiline = true;
            this.txt_str2.Name = "txt_str2";
            this.txt_str2.Size = new System.Drawing.Size(293, 41);
            this.txt_str2.TabIndex = 5;
            // 
            // Frm_Main
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(329, 219);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Name = "Frm_Main";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Sorting one dimensional array by bubble sorting";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button btn_sort;
        private System.Windows.Forms.Button btn_Generate;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.TextBox txt_str;
        private System.Windows.Forms.TextBox txt_str2;
    }
}

Topics: C# Windows