C# 冒泡排序

来源:互联网 发布:淘宝上买电信卡 编辑:程序博客网 时间:2024/05/03 13:20
  1. using System;
  2. public class BubbleSort
  3. {
  4.    public void Sort(int[] list)
  5.    {
  6.      int temp;
  7.      for(int i=0;i<list.Length-1;i++)
  8.      {
  9.        for(int j=i+1;j<list.Lenght;j++)
  10.        {
  11.          if (list[j]<list[i])
  12.          {
  13.             temp=list[j];
  14.             list[j]=list[i];
  15.             list[i]=temp;
  16.          }
  17.        } 
  18.      }
  19.    }
  20.   
  21.    public static void Main(string[] arg)
  22.    {
  23.       int testArray=new int[]{1,11,3,32,22,12,33,55};
  24.       BubbleSort bs=new BubbleSort();
  25.       bs.Sort(testArray);
  26.       for(int i=0;i<testArray.Length;i++)
  27.       {
  28.         Console.WriteLine("{0}",testArray[i]);
  29.       }
  30.       Console.ReadKey();
  31.    }
  32. }
原创粉丝点击