更新於 2024/11/01閱讀時間約 9 分鐘

補充教學-氣泡排序法

    氣泡排序法(Bubble Sort)執行範例

    raw-image


    BubbleSample.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="BubbleSample.aspx.cs" Inherits="BubbleSample" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    原本亂數產生的陣列:<br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <br /><br />
    排序後的陣列:<br />
    <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
    <br /><br />
    由小到大排序每一次的過程<br />
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label>

    <br /><br />
    <!--排序執行時間-->
    <asp:Label ID="Label4" runat="server" Text=""></asp:Label>
    </div>
    </form>
    </body>
    </html>

    BubbleSample.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
    int[] ary = new int[5]; //宣告陣列大小為10

    Random rnd = new Random(); //產生亂數初始值

    for (int i = 0; i < ary.Length; i++)
    {
    ary[i] = rnd.Next(1, 10); //亂數產生,亂數產生的範圍是1~9
    }

    //顯示原本陣列內容
    showArray(Label1, ary);
    //排序
    BubbleSort(Label2, ary);
    //顯示陣列由小到大排序後結果
    showArray(Label3, ary);
    }

    void BubbleSort(Label lab, int[] ary)
    {
    System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
    stopWatch.Start(); //計算程式時間開始

    int counter = 0;

    for (int i = 0; i < ary.Length; i++)
    {
    //因為下面比較會使用到j+1,所以j最大只能到陣列的倒數第二個,不然發生錯誤(陣列會超出範圍)
    for (int j = 0; j < ary.Length-1; j++)
    {
    lab.Text += $"ary[{j}] = {ary[j]},ary[{j+1}] = {ary[j+1]},";

    //由小到大;如果要改為由大到小,把 ary[j < ary[j+1] 改為 ary[j] > ary[j+1]
    if (ary[j] > ary[j+1])
    {
    lab.Text += "swap,";
    swap(ary, j, j+1);
    }
    else
    {
    //顯示空格,排版用,畫面比較整齊
    lab.Text += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,";
    }
    counter++;
    lab.Text += $"第{counter.ToString("00")}次排序後結果: ";
    showArray(lab, ary, true, j);
    }
    lab.Text += $"此輪(for j 迴圈)結束會找到ary[{ary.Length-1-i}]是陣列 0 - {ary.Length - 1 - i}中最大的數值 {ary[ary.Length - 1 - i]}<br>";
    }

    stopWatch.Stop(); //計算程式時間結束

    Label4.Text += $"排序執行次數: {counter} <br>";

    Label4.Text += $"排序執行時間(秒):{(stopWatch.Elapsed.TotalMilliseconds / 1000).ToString("")} <br>";
    }

    //兩個值互相交換
    void swap(int[] ary, int value1, int value2)
    {
    int tmp = ary[value2];
    ary[value2] = ary[value1];
    ary[value1] = tmp;
    }

    //顯示陣列內容
    //IsAdd:是一個選擇(optional)參數,可用可不用,預設值為false
    //markIndex:會將陣列中的第markIndex與markIndex+1元素變成紅色,預設值為-1 (陣列起始值為0)
    void showArray(Label lab , int[] ary, bool IsAdd = false, int markIndex = -1)
    {
    string str = string.Empty;

    for (int i = 0; i < ary.Length; i++)
    {
    if(markIndex > -1 && (i == markIndex || i == (markIndex+1)))
    {
    str += $"<font color=red>{ary[i]}</font>,";
    }
    else
    {
    str += ary[i] + ",";
    }
    }

    if(IsAdd == false)
    {
    lab.Text = str;
    }
    else
    {
    lab.Text += str + "<br>";
    }
    }

    執行結果:


    還有其他的排序方式,例如

    • Selection Sort
    • Insertion Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort

    還有很多種排序方式,可以上網查詢關鍵字或是查詢"資料結構"(Algorithm)

    範例網站:https://medium.com/engineering-hub/https-medium-com-engineering-hub-sorting-algorithms-in-csharp-and-java-4615f6f87696


    分享至
    成為作者繼續創作的動力吧!
    © 2024 vocus All rights reserved.