天天看點

C#排序算法 之 冒泡排序

  1. using System;   
  2. namespace BubbleSorter    
  3. {    
  4.     public class BubbleSorter    
  5.     {    
  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.     }    
  28.     public class MainClass    
  29.         public static void Main()    
  30.             int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};    
  31.             BubbleSorter sh=new BubbleSorter();    
  32.             sh.Sort(iArrary);    
  33.             for(int m=0;m<iArrary.Length;m++)    
  34.             {   
  35.                 Console.Write("{0} ",iArrary[m]);    
  36.                 Console.WriteLine();    
  37.             }   

繼續閱讀