C# 实现 冒泡排序

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
/***
*
* 冒泡排序
*
*
* * *
*/
namespace TestConsole
{


class Program
{
static void Main(string[] args)
{
int[] array = {32,24 ,21,20,18,3,8,57};
BubbleSort(array);

foreach (int index in array)
{
Console.WriteLine(index
+ "\t");

}
}

public static void BubbleSort(int[] array)
{
int temp = 0;

for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - 1 - i; j++)
{
if (array[j] > array[j + 1])//如果前一个数比后一个数大,则交换
{
temp
= array[j + 1];

array[j
+ 1] = array[j];

array[j]
= temp;


}
}
}

}
}
}


// 3,8,18,20,21,24,32,57

posted on 2011-09-14 23:37  别人叫我军师  阅读(194)  评论(0编辑  收藏  举报