午夜无码人妻aⅴ大片色欲张津瑜,国产69久久久欧美黑人A片,色妺妺视频网,久久久久国产综合AV天堂

C#實現(xiàn)冒泡排序的方法

這篇文章運用簡單易懂的例子給大家介紹C#實現(xiàn)冒泡排序的方法,代碼非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

德城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

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

比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。

對每一對相鄰元素做同樣的工作,從開始第一對到結(jié)尾的最后一對。在這一點,最后的元素應(yīng)該會是最大的數(shù)。

針對所有的元素重復(fù)以上的步驟,除了最后一個。

持續(xù)每次對越來越少的元素重復(fù)上面的步驟,直到?jīng)]有任何一對數(shù)字需要比較。

冒泡排序核心就是比較方法,冒泡排序的比較方法顧名思義就是像氣泡一樣,最大(或者最?。┑臄?shù)往上冒。

普通比較幾個數(shù),我們可以用if(a>b)然后c=a;b=a 。。。。這類方法,把大數(shù)暫存到c中,然后小的數(shù)存到a中

原本的比較小的數(shù)繼續(xù)跟其他數(shù)比較。冒泡排序也是如此,不過冒泡排序比較的數(shù)據(jù)比較多,需要用到for循環(huán),
一個數(shù)比較完,比較下一個,一直循環(huán)到最后一個,先找出最大的數(shù),然后再找第二大的,以此類推。
實現(xiàn)程序如下:

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;//定義數(shù)組字段

        private Random G_Random = new Random();//創(chuàng)建隨機數(shù)對象

        private void btn_sort_Click(object sender, EventArgs e)
        {
            if (G_int_value != null)
            {
                //定義兩個int類型的變量,分別用來表示數(shù)組下標和存儲新的數(shù)組元素
                int j, temp;
                for (int i = 0; i < G_int_value.Length - 1; i++)//根據(jù)數(shù)組下標的值遍歷數(shù)組元素
                {
                    j = i + 1;
                id://定義一個標識,以便從這里開始執(zhí)行語句
                    if (G_int_value[i] > G_int_value[j])//判斷前后兩個數(shù)的大小
                    {
                        temp = G_int_value[i];//將比較后大的元素賦值給定義的int變量
                        G_int_value[i] = G_int_value[j];//將后一個元素的值賦值給前一個元素
                        G_int_value[j] = temp;//將int變量中存儲的元素值賦值給后一個元素
                        goto id;//返回標識,繼續(xù)判斷后面的元素
                    }
                    else
                        if (j < G_int_value.Length - 1)//判斷是否執(zhí)行到最后一個元素
                        {
                            j++;//如果沒有,則再往后判斷 
                            goto id;//返回標識,繼續(xù)判斷后面的元素
                        }
                }
                txt_str2.Clear();//清空控件內(nèi)字符串
                foreach (int i in G_int_value)//遍歷字符串集合
                {
                    txt_str2.Text += i.ToString() + ", ";//向控件內(nèi)添加字符串
                }
            }
            else
            {
                MessageBox.Show("首先應(yīng)當(dāng)生成數(shù)組,然后再進行排序。", "提示!");
            }
        }

        private void btn_Generate_Click(object sender, EventArgs e)
        {
            G_int_value = new int[G_Random.Next(10, 20)];//生成隨機長度數(shù)組
            for (int i = 0; i < G_int_value.Length; i++)//遍歷數(shù)組
            {
                G_int_value[i] = G_Random.Next(0, 100);//為數(shù)組賦隨機數(shù)值
            }
            txt_str.Clear();//清空控件內(nèi)字符串
            foreach (int i in G_int_value)//遍歷字符串集合
            {
                txt_str.Text += i.ToString() + ", ";//向控件內(nèi)添加字符串

            }
        }
    }
}

設(shè)計代碼如下:

namespace BubbleUpSort
{
    partial class Frm_Main
    {
        /// <summary>
        /// 必需的設(shè)計器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設(shè)計器生成的代碼

        /// <summary>
        /// 設(shè)計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內(nèi)容。
        /// </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 = "生成隨機數(shù)組";
            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 = "生成隨機數(shù)組";
            // 
            // 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 = "排序隨機數(shù)組";
            // 
            // 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 = "使用冒泡排序法對一維數(shù)組進行排序";
            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;
    }
}

關(guān)于C#實現(xiàn)冒泡排序的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)站欄目:C#實現(xiàn)冒泡排序的方法
文章分享:http://www.ekvhdxd.cn/article48/ghdgep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站收錄、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè)用戶體驗、網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)