因为之前学过c++、Java,在这里就不重复思想了,直接给出代码。
在C#中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace int的冒泡排序
{
class Program
{
static void sort(int[] sortArray)
{
bool swaped = true;
do
{
swaped = false;
for (int i = 0; i <sortArray.Length - 1; i++)
{
if (sortArray[i] > sortArray[i + 1])
{
int temp = sortArray[i];
sortArray[i] = sortArray[i + 1];
sortArray[i + 1] = temp;
swaped = true;
}
}
} while (swaped);
}
static void Main(string[] args)
{
int[] sortArray = new int[]{23, 45, 67, 98, 43, 12, 1223, 1, 4, 78, 34};
sort(sortArray);
foreach (var temp in sortArray )
{
Console.WriteLine(temp+" ");
}
Console.ReadKey();
}
}
}
仅供参考。