程式初學者必學的泡沫排序法使用C#
泡沫排序法是程式設計初學這必學的程式,使用泡沫排序法可以把一串數字按照順序來排列,過去我面試的時候也常常有遇到很多公司會考泡沫排序法,這是一個很簡單也很重要的考題,以下我就使用C#來撰寫泡沫排序法:
static void Main(string[] args)
{
int[] Data = new int[5] { 12, 5, 8, 6, 2};
int temp;
for(int i=0;i<5;i++)
for(int j=i+1;j<5;j++)
{
if(Data[i]>Data[j])
{
temp = Data[i];
Data[i] = Data[j];
Data[j] = temp;
}
}
foreach(int x in Data)
{
Console.Write(x + " ");
}
}
執行結果:
2 5 6 8 12
之前我有用VB寫過泡沫排序法,大家可以參考這一篇: