補充教學-氣泡排序法

更新 發佈閱讀 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>";
}
}

執行結果:

raw-image


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

  • 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


留言
avatar-img
Phyxsius
0會員
19內容數
FIRE
你可能也想看
Thumbnail
在 vocus 與你一起探索內容、發掘靈感的路上,我們又將啟動新的冒險——vocus App 正式推出! 現在起,你可以在 iOS App Store 下載全新上架的 vocus App。 無論是在通勤路上、日常空檔,或一天結束後的放鬆時刻,都能自在沈浸在內容宇宙中。
Thumbnail
在 vocus 與你一起探索內容、發掘靈感的路上,我們又將啟動新的冒險——vocus App 正式推出! 現在起,你可以在 iOS App Store 下載全新上架的 vocus App。 無論是在通勤路上、日常空檔,或一天結束後的放鬆時刻,都能自在沈浸在內容宇宙中。
Thumbnail
市場經驗拉長之後,很多投資人都會遇到同一個問題:不是方向看錯,而是部位太集中個股,常常跟大趨勢脫節。 早年的台股環境,中小股非常吃香,反而權值股不動,但QE量化寬鬆後,特別是疫情之後,後疫情時代,鈔票大量在股市走動,這些大資金只能往權值股走,因此早年小P的策略偏向中小型個股,但近年AI興起,高技術
Thumbnail
市場經驗拉長之後,很多投資人都會遇到同一個問題:不是方向看錯,而是部位太集中個股,常常跟大趨勢脫節。 早年的台股環境,中小股非常吃香,反而權值股不動,但QE量化寬鬆後,特別是疫情之後,後疫情時代,鈔票大量在股市走動,這些大資金只能往權值股走,因此早年小P的策略偏向中小型個股,但近年AI興起,高技術
Thumbnail
中學數學基礎練習—分組分解法
Thumbnail
中學數學基礎練習—分組分解法
Thumbnail
高中數學主題練習—二階行列式
Thumbnail
高中數學主題練習—二階行列式
Thumbnail
高中數學主題練習—二階行列式
Thumbnail
高中數學主題練習—二階行列式
Thumbnail
高中數學主題練習—配方法
Thumbnail
高中數學主題練習—配方法
Thumbnail
高中數學主題練習—配方法
Thumbnail
高中數學主題練習—配方法
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—指數律基本練習
Thumbnail
高中數學主題練習—指數律基本練習
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News