泡沫排序法使用方法的程式語法
上次我跟大家介紹過怎麼用C#寫泡沫排序法,今天在跟大家一樣講C#寫泡沫排序法,但是有使用方法,這樣的寫法會比較好一點,感覺比較有水準,程式如下:
static void Main(string[] args)
{
int[] array = { 1, 12, 5, 9, 6, 8 };
array = sortarray(array);
printNumber(array);
}
static int[] sortarray(int[] a) //泡沫排序方法
{
int i, j, temp;
for(i=0;i<=a.GetUpperBound(0);i++)
{
for (j = i; j <= a.GetUpperBound(0); j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
static void printNumber(int[] a) //印出數字
{
foreach(int n in a)
{
Console.Write(n + "\t");
}
Console.WriteLine();
}
相關文章: