内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

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

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

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

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

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

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

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

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

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

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

原本的比較小的數(shù)繼續(xù)跟其他數(shù)比較。冒泡排序也是如此,不過(guò)冒泡排序比較的數(shù)據(jù)比較多,需要用到for循環(huán),
一個(gè)數(shù)比較完,比較下一個(gè),一直循環(huán)到最后一個(gè),先找出最大的數(shù),然后再找第二大的,以此類(lèi)推。
實(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)建隨機(jī)數(shù)對(duì)象

        private void btn_sort_Click(object sender, EventArgs e)
        {
            if (G_int_value != null)
            {
                //定義兩個(gè)int類(lèi)型的變量,分別用來(lái)表示數(shù)組下標(biāo)和存儲(chǔ)新的數(shù)組元素
                int j, temp;
                for (int i = 0; i < G_int_value.Length - 1; i++)//根據(jù)數(shù)組下標(biāo)的值遍歷數(shù)組元素
                {
                    j = i + 1;
                id://定義一個(gè)標(biāo)識(shí),以便從這里開(kāi)始執(zhí)行語(yǔ)句
                    if (G_int_value[i] > G_int_value[j])//判斷前后兩個(gè)數(shù)的大小
                    {
                        temp = G_int_value[i];//將比較后大的元素賦值給定義的int變量
                        G_int_value[i] = G_int_value[j];//將后一個(gè)元素的值賦值給前一個(gè)元素
                        G_int_value[j] = temp;//將int變量中存儲(chǔ)的元素值賦值給后一個(gè)元素
                        goto id;//返回標(biāo)識(shí),繼續(xù)判斷后面的元素
                    }
                    else
                        if (j < G_int_value.Length - 1)//判斷是否執(zhí)行到最后一個(gè)元素
                        {
                            j++;//如果沒(méi)有,則再往后判斷 
                            goto id;//返回標(biāo)識(shí),繼續(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ù)組,然后再進(jìn)行排序。", "提示!");
            }
        }

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

            }
        }
    }
}

設(shè)計(jì)代碼如下:

namespace BubbleUpSort
{
    partial class Frm_Main
    {
        /// <summary>
        /// 必需的設(shè)計(jì)器變量。
        /// </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è)計(jì)器生成的代碼

        /// <summary>
        /// 設(shè)計(jì)器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內(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 = "生成隨機(jī)數(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 = "生成隨機(jī)數(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 = "排序隨機(jī)數(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 = "使用冒泡排序法對(duì)一維數(shù)組進(jìn)行排序";
            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#實(shí)現(xiàn)冒泡排序的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

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

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

廣告

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

外貿(mào)網(wǎng)站建設(shè)
长岛县| 东丽区| 麻栗坡县| 宁阳县| 鄢陵县| 甘德县| 泰和县| 葵青区| 大埔县| 巴中市| 上虞市| 临清市| 探索| 比如县| 正安县| 桂林市| 茌平县| 噶尔县| 华蓥市| 新化县| 原阳县| 盱眙县| 巫溪县| 睢宁县| 晴隆县| 巩义市| 繁昌县| 固安县| 察隅县| 涪陵区| 翁牛特旗| 桐柏县| 宿州市| 会宁县| 通州市| 祥云县| 滨州市| 古浪县| 比如县| 额尔古纳市| 八宿县|