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

C# 实现 冒泡排序

2011-09-14 23:37 260 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: