補充教學-氣泡排序法

更新於 發佈於 閱讀時間約 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
留言分享你的想法!
avatar-img
Phyxsius
0會員
19內容數
FIRE
你可能也想看
Thumbnail
孩子寫功課時瞇眼?小心近視!這款喜光全光譜TIONE⁺光健康智慧檯燈,獲眼科院長推薦,網路好評不斷!全光譜LED、180cm大照明範圍、5段亮度及色溫調整、350度萬向旋轉,讓孩子學習更舒適、保護眼睛!
Thumbnail
孩子寫功課時瞇眼?小心近視!這款喜光全光譜TIONE⁺光健康智慧檯燈,獲眼科院長推薦,網路好評不斷!全光譜LED、180cm大照明範圍、5段亮度及色溫調整、350度萬向旋轉,讓孩子學習更舒適、保護眼睛!
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
中學數學基礎練習—分組分解法
Thumbnail
中學數學基礎練習—分組分解法
Thumbnail
高中數學主題練習—二階行列式
Thumbnail
高中數學主題練習—二階行列式
Thumbnail
高中數學主題練習—二階行列式
Thumbnail
高中數學主題練習—二階行列式
Thumbnail
高中數學主題練習—配方法
Thumbnail
高中數學主題練習—配方法
Thumbnail
高中數學主題練習—配方法
Thumbnail
高中數學主題練習—配方法
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—指數律基本練習
Thumbnail
高中數學主題練習—指數律基本練習
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
Thumbnail
高中數學主題練習—根式化簡
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News