C#实现冒泡排序的方法

发布时间:2020-07-02 14:11:59 作者:元一
来源:亿速云 阅读:165

这篇文章运用简单易懂的例子给大家介绍C#实现冒泡排序的方法,代码非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

冒泡排序算法的原理如下:

比较相邻的元素。如果第一个比第二个大,就交换他们两个。

对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。

针对所有的元素重复以上的步骤,除了最后一个。

持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

冒泡排序核心就是比较方法,冒泡排序的比较方法顾名思义就是像气泡一样,最大(或者最小)的数往上冒。

普通比较几个数,我们可以用if(a>b)然后c=a;b=a 。。。。这类方法,把大数暂存到c中,然后小的数存到a中

原本的比较小的数继续跟其他数比较。冒泡排序也是如此,不过冒泡排序比较的数据比较多,需要用到for循环,
一个数比较完,比较下一个,一直循环到最后一个,先找出最大的数,然后再找第二大的,以此类推。
实现程序如下:

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 BubbleUpSort
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private int[] G_int_value;//定义数组字段

        private Random G_Random = new Random();//创建随机数对象

        private void btn_sort_Click(object sender, EventArgs e)
        {
            if (G_int_value != null)
            {
                //定义两个int类型的变量,分别用来表示数组下标和存储新的数组元素
                int j, temp;
                for (int i = 0; i < G_int_value.Length - 1; i++)//根据数组下标的值遍历数组元素
                {
                    j = i + 1;
                id://定义一个标识,以便从这里开始执行语句
                    if (G_int_value[i] > G_int_value[j])//判断前后两个数的大小
                    {
                        temp = G_int_value[i];//将比较后大的元素赋值给定义的int变量
                        G_int_value[i] = G_int_value[j];//将后一个元素的值赋值给前一个元素
                        G_int_value[j] = temp;//将int变量中存储的元素值赋值给后一个元素
                        goto id;//返回标识,继续判断后面的元素
                    }
                    else
                        if (j < G_int_value.Length - 1)//判断是否执行到最后一个元素
                        {
                            j++;//如果没有,则再往后判断 
                            goto id;//返回标识,继续判断后面的元素
                        }
                }
                txt_str2.Clear();//清空控件内字符串
                foreach (int i in G_int_value)//遍历字符串集合
                {
                    txt_str2.Text += i.ToString() + ", ";//向控件内添加字符串
                }
            }
            else
            {
                MessageBox.Show("首先应当生成数组,然后再进行排序。", "提示!");
            }
        }

        private void btn_Generate_Click(object sender, EventArgs e)
        {
            G_int_value = new int[G_Random.Next(10, 20)];//生成随机长度数组
            for (int i = 0; i < G_int_value.Length; i++)//遍历数组
            {
                G_int_value[i] = G_Random.Next(0, 100);//为数组赋随机数值
            }
            txt_str.Clear();//清空控件内字符串
            foreach (int i in G_int_value)//遍历字符串集合
            {
                txt_str.Text += i.ToString() + ", ";//向控件内添加字符串

            }
        }
    }
}

设计代码如下:

namespace BubbleUpSort
{
    partial class Frm_Main
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </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 = "冒泡排序";
            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 = "生成随机数组";
            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 = "生成随机数组";
            // 
            // 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 = "排序随机数组";
            // 
            // 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 = "使用冒泡排序法对一维数组进行排序";
            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;
    }
}

关于C#实现冒泡排序的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. C#实现简单的冒泡排序
  2. java实现数组冒泡排序的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

冒泡排序

上一篇:轻松精通数据库管理之道——运维巡检之二(数据库)

下一篇:开始新的学习之旅--PHP开发学习

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》