代码改变世界

C#排序算法 之 冒泡排序

2007-06-25 13:32  乱世文章  阅读(184)  评论(0编辑  收藏  举报
  1. using System;   
  2.   
  3. namespace BubbleSorter    
  4. {    
  5.     public class BubbleSorter    
  6.     {    
  7.         public void Sort(int [] list)    
  8.         {    
  9.             int i,j,temp;    
  10.             bool done=false;    
  11.             j=1;    
  12.             while((j<list.Length)&&(!done))    
  13.             {    
  14.                 done=true;    
  15.                 for(i=0;i<list.Length-j;i++)    
  16.                 {    
  17.                     if(list[i]>list[i+1])    
  18.                     {    
  19.                         done=false;    
  20.                         temp=list[i];    
  21.                         list[i]=list[i+1];    
  22.                         list[i+1]=temp;    
  23.                     }    
  24.                 }    
  25.                 j++;    
  26.             }    
  27.         }    
  28.     }    
  29.   
  30.     public class MainClass    
  31.     {    
  32.         public static void Main()    
  33.         {    
  34.             int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};    
  35.             BubbleSorter sh=new BubbleSorter();    
  36.             sh.Sort(iArrary);    
  37.             forint m=0;m<iArrary.Length;m++)    
  38.             {   
  39.                 Console.Write("{0} ",iArrary[m]);    
  40.                 Console.WriteLine();    
  41.             }   
  42.         }    
  43.     }    
  44. }