您的位置:首页 > 编程语言 > C#

C# 冒泡排序

2015-07-06 11:42 351 查看
class Program
{
static void swap( ref int  atemp, ref int  btemp)//注意ref的使用
{
int temp = atemp;
atemp = btemp;
btemp = temp;
}
static void Main(string[] args)
{
int temp=0;
int[]arr={23,44,66,76,98,11,3,9,7};
Console.WriteLine("排序前的数组:");
foreach(int item in arr)
{
Console.Write(item+" ");
}
Console.WriteLine();
for(int i=0;i<arr.Length-1;i++)
{
for(int j=0;j<arr.Length-1-i;j++)
{
if (arr[j] > arr[j + 1])
swap( ref  arr[j], ref arr[j + 1]);
}
}
Console.WriteLine("排序后的数组:");
foreach(int item in arr)
{
Console.Write(item+" ");
}
Console.WriteLine();
Console.ReadKey();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: