這個排序演算法主要是利用二個迴圈,一個是要執行的回合數,另一個是真正比對數列資料,一次比較相臨的二個數,大的放右側,小的放左側,則可得最後一個數為最大數,則此數不再比較,以此類推,可以排出一個左小右大的數列。
演算過程的畫面如下程式畫面:
要排列的數列串是2,3,8,1,5,9,10,12,14
這是執行的過程及結果,一共執行了八回合
其中紅色字為需要交換字、藍色是正在比較的二個數、綠色是已排好的數,不會再進行比較
演算法的原始碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { public static void BubbleSort(int[] list) { int len = list.Length; for(int i = 1;i<=len-1;i++)//執行的回數 for (int j = 1; j <= len - i; j++)//執行的次數 { if (list[j] < list[j - 1]) { //二數交換 int temp = list[j]; list[j] = list[j - 1]; list[j - 1] = temp; } } } static void Main(string[] args) { int[] list = { 2,3,1,6,2,9,4,1,7}; Console.WriteLine("排序前數列"); for (int i = 0; i < list.Length; i++) Console.Write(list[i]+" "); //氣泡排序 BubbleSort(list); Console.WriteLine("\r\n排序後數列"); for (int i = 0; i < list.Length; i++) Console.Write(list[i] + " "); Console.ReadLine(); } } }
執行結果如下:
執行動作過程的程式原始碼如下:
Form1
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 WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click_1(object sender, EventArgs e) { string value = this.textBox1.Text; if (value.Equals("")) { MessageBox.Show(this, "請先輸入數字串"); } else { string[] strnum = value.Split(new Char[]{','}); int[] num = new int[strnum.Length]; for (int i = 0; i < num.Length; i++) { num[i] = System.Convert.ToInt32(strnum[i]); } Form2 form2 = new Form2(num); form2.Visible = true; } } } }
Form2
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 WindowsFormsApplication1 { public partial class Form2 : Form { private int[] num; private Font font = new Font("Verdana", 9); //Font font_word = new Font("Verdana", 12); private SolidBrush changebrush = new SolidBrush(Color.Red); private SolidBrush nochangebrush = new SolidBrush(Color.Blue); private SolidBrush brush_ok = new SolidBrush(Color.Green); private SolidBrush brush_word = new SolidBrush(Color.Black); private SolidBrush brush_word2 = new SolidBrush(Color.Pink); private int leftPoint = 20; private int topPoint = 20; public Form2() { InitializeComponent(); num = new int[0]; } public Form2(int[] num) { InitializeComponent(); this.num = num; } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; int len = num.Length; int pTop = 10; for (int i = 1; i <= len - 1; i++)//比較回數 { g.DrawString("i=" + i, font, brush_word, 0, pTop + topPoint); int left = 20; for (int j = 1; j <= len - i; j++)//比較次數 { if ((j-1) % 3 == 0) { pTop = pTop + topPoint; left = 20; } left = left + 40; g.DrawString("j=" + j, font, brush_word2, left, pTop); //if (num[j] < num[j - 1]) //{ //left = draw(num[j], num[j - 1], pTop, left + leftPoint, len - i, g); //Console.WriteLine(left); //pTop = rev[0]; //left = rev[1]; //} //else //{ left = draw(j, j - 1, pTop, left + leftPoint, len - i,(num[j] < num[j - 1]), g); //Console.WriteLine(left); // pTop = rev[0]; //left = rev[1]; // } //二數交換 if(num[j]<num[j-1]) { int temp = num[j]; num[j] = num[j - 1]; num[j - 1] = temp; } } } } //索引位置1,索引位置2,開始的top,開始的left,已經排好的右側數列索引位置,是否要交換,畫筆 private int draw(int swap1, int swap2, int startTop, int startLeft,int okIndex,Boolean haschange,Graphics g) { int left = startLeft; int top = startTop; SolidBrush brush ; for (int i = 0; i < num.Length; i++) { if (okIndex < i) { brush = brush_ok; } else { if (i == swap1 || i==swap2) { if (haschange) brush = changebrush;//有要交換的顏色 else brush = nochangebrush;//沒有要交換的顏色 } else { brush = brush_word; } } left = left + leftPoint; g.DrawString(num[i].ToString(), font, brush, left, top); } return left; } } }