冒泡排序 C#
主程序入口
class Program
{
static void Main(string[] args)
{
int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };//定义数组
BubbleSorter sh = new BubbleSorter();
sh.Sort(iArrary);
for (int m = 0; m < iArrary.Length; m++)//输出结果
Console.Write("{0} ", iArrary[m]);
Console.ReadLine();
}
{
static void Main(string[] args)
{
int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };//定义数组
BubbleSorter sh = new BubbleSorter();
sh.Sort(iArrary);
for (int m = 0; m < iArrary.Length; m++)//输出结果
Console.Write("{0} ", iArrary[m]);
Console.ReadLine();
}
}
冒泡排序方法
1 class BubbleSorter
2 {
3 /// <summary>
4 /// 冒泡排序
5 /// </summary>
6 public void Sort(int[] list)
7 {
8 int i, j, temp;
9 bool done = false;
10 j = 1;
11 while ((j < list.Length) && (!done))//判断长度
12 {
13 done = true;
14 for (i = 0; i < list.Length - j; i++)
15 {
16 if (list[i] > list[i + 1])
17 {
18 done = false;
19 temp = list[i];
20 list[i] = list[i + 1];//交换数据
21 list[i + 1] = temp;
22 }
23 }
24 j++;
25 }
26 }
2 {
3 /// <summary>
4 /// 冒泡排序
5 /// </summary>
6 public void Sort(int[] list)
7 {
8 int i, j, temp;
9 bool done = false;
10 j = 1;
11 while ((j < list.Length) && (!done))//判断长度
12 {
13 done = true;
14 for (i = 0; i < list.Length - j; i++)
15 {
16 if (list[i] > list[i + 1])
17 {
18 done = false;
19 temp = list[i];
20 list[i] = list[i + 1];//交换数据
21 list[i + 1] = temp;
22 }
23 }
24 j++;
25 }
26 }
27 }
乌龟才背着房子过一辈子