{
/*假定有以下數組*/
int[] m =
{ 2, 8, 43, 3, 33, 1, 35, 34, 6, 9 };
/*執行冒泡排序*/
int[] n = sort(m);
/*過經冒泡算法後,從大到小依次輸出數字*/
for (int i = 0; i < m.Length ; i++)
{
MessageBox.Show(n[i].ToString());
}
}
/* 冒泡排序算法*/
protected int[] sort(int[] m)
{
int intLenth = m.Length;
/*執行intLenth次*/
for (int i = 0; i < intLenth; i++)
{
for (int j = 0; j < intLenth - i - 1; j++)
{
int a = m[j];
int b = m[j + 1];
if (a < b)
{
m[j] = b;
m[j + 1] = a;
}
}
return m;
}