C#排序演算法大全

來源:互聯網
上載者:User

C#排序演算法大全(轉載)

   冒泡排序  
   
    本人用了C#開發出冒泡排序演算法。希望能為C#語言的學習者帶來一些益處。不要忘了,學語言要花大力氣學資料結構

和演算法。  
   
  using   System;    
   
  namespace   BubbleSorter  
  {  
  public   class   BubbleSorter  
  {  
  public   void   Sort(int   []   list)  
  {  
  int   i,j,temp;  
  bool   done=false;  
  j=1;  
  while((j<list.Length)&&(!done))  
  {  
  done=true;  
  for(i=0;i<list.Length-j;i++)  
  {  
  if(list[i]>list[i+1])  
  {  
  done=false;  
  temp=list[i];  
  list[i]=list[i+1];  
  list[i+1]=temp;  
  }  
  }  
  j++;  
  }  
   
   
  }  
  }  
  public   class   MainClass  
  {    
  public   static   void   Main()  
  {  
  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.WriteLine();  
  }  
  }  
  }  
     
   
    選擇排序    
    本人用了C#開發出選擇排序演算法。希望能為C#語言的學習者帶來一些益處。不要忘了,學語言要花大力氣學資料結構

和演算法。  
   
   
  using   System;  
   
   
  namespace   SelectionSorter  
  {  
  public   class   SelectionSorter  
  {    
  private   int   min;  
  public   void   Sort(int   []   list)  
  {  
  for(int   i=0;i<list.Length-1;i++)  
  {  
  min=i;  
  for(int   j=i+1;j<list.Length;j++)  
  {  
  if(list[j]<list[min])  
  min=j;  
  }  
  int   t=list[min];  
  list[min]=list[i];  
  list[i]=t;  
  }  
   
   
  }  
  }  
  public   class   MainClass  
  {    
  public   static   void   Main()  
  {  
  int[]   iArrary=new   int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};  
  SelectionSorter   ss=new   SelectionSorter();  
  ss.Sort(iArrary);  
  for(int   m=0;m<iArrary.Length;m++)  
  Console.Write("{0}   ",iArrary[m]);    
  Console.WriteLine();  
   
   
  }  
  }  
  }  
     
   
    插入排序    
   
    插入排序演算法。對想提高C#語言編程能力的朋友,我們可以互相探討一下。如:下面的程式,並沒有實現多態,來,

幫它實現一下。  
   
   
  using   System;  
   
   
  namespace   InsertionSorter  
  {  
  public   class   InsertionSorter  
  {  
  public   void   Sort(int   []   list)  
  {  
  for(int   i=1;i<list.Length;i++)  
  {  
  int   t=list[i];  
  int   j=i;  
  while((j>0)&&(list[j-1]>t))  
  {  
  list[j]=list[j-1];  
  --j;  
  }  
  list[j]=t;  
  }  
   
   
  }  
  }  
  public   class   MainClass  
  {    
  public   static   void   Main()  
  {  
  int[]   iArrary=new   int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};  
  InsertionSorter   ii=new   InsertionSorter();  
  ii.Sort(iArrary);  
  for(int   m=0;m<iArrary.Length;m++)  
  Console.Write("{0}",iArrary[m]);    
  Console.WriteLine();  
  }  
  }  
  }  
     
   
    希爾排序    
   
    希爾排序是將組分段,進行插入排序.   對想提高C#語言編程能力的朋友,我們可以互相探討一下。如:下面的程式

,並沒有實現多態,來,幫它實現一下。  
   
   
  using   System;    
   
   
  namespace   ShellSorter  
  {  
  public   class   ShellSorter  
  {  
  public   void   Sort(int   []   list)  
  {  
  int   inc;  
  for(inc=1;inc<=list.Length/9;inc=3*inc+1);  
  for(;inc>0;inc/=3)  
  {  
  for(int   i=inc+1;i<=list.Length;i+=inc)  
  {  
  int   t=list[i-1];  
  int   j=i;  
  while((j>inc)&&(list[j-inc-1]>t))  
  {  
  list[j-1]=list[j-inc-1];  
  j-=inc;  
  }  
  list[j-1]=t;  
  }  
  }  
  }  
  }  
  public   class   MainClass  
  {    
  public   static   void   Main()  
  {  
  int[]   iArrary=new   int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};  
  ShellSorter   sh=new   ShellSorter();  
  sh.Sort(iArrary);  
  for(int   m=0;m<iArrary.Length;m++)  
  Console.Write("{0}   ",iArrary[m]);    
  Console.WriteLine();  
  }  
  }  
  }    

*****************************************************************
  冒泡排序

  本人用了C#開發出冒泡排序演算法。希望能為C#語言的學習者帶來一些益處。不要忘了,學語言要花大力氣學資料結構和

演算法。

using System;

namespace BubbleSorter
{
public class BubbleSorter
{
public void Sort(int [] list)
{
int i,j,temp;
bool done=false;
j=1;
while((j<list.Length)&&(!done))
{
done=true;
for(i=0;i<list.Length-j;i++)
{
if(list[i]>list[i+1])
{
done=false;
temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
}
}
j++;
}

}
}
public class MainClass
{
public static void Main()
{
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.WriteLine();
}
}
}
 

  選擇排序

  本人用了C#開發出選擇排序演算法。希望能為C#語言的學習者帶來一些益處。不要忘了,學語言要花大力氣學資料結構和

演算法。

using System;

namespace SelectionSorter
{
public class SelectionSorter
{
private int min;
public void Sort(int [] list)
{
for(int i=0;i<list.Length-1;i++)
{
min=i;
for(int j=i+1;j<list.Length;j++)
{
if(list[j]<list[min])
min=j;
}
int t=list[min];
list[min]=list[i];
list[i]=t;
}

}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
SelectionSorter ss=new SelectionSorter();
ss.Sort(iArrary);
for(int m=0;m<iArrary.Length;m++)
Console.Write("{0} ",iArrary[m]);
Console.WriteLine();

}
}
}
 

  插入排序

  插入排序演算法。對想提高C#語言編程能力的朋友,我們可以互相探討一下。如:下面的程式,並沒有實現多態,來,幫

它實現一下。

using System;

namespace InsertionSorter
{
public class InsertionSorter
{
public void Sort(int [] list)
{
for(int i=1;i<list.Length;i++)
{
int t=list[i];
int j=i;
while((j>0)&&(list[j-1]>t))
{
list[j]=list[j-1];
--j;
}
list[j]=t;
}

}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
InsertionSorter ii=new InsertionSorter();
ii.Sort(iArrary);
for(int m=0;m<iArrary.Length;m++)
Console.Write("{0}",iArrary[m]);
Console.WriteLine();
}
}
}
 

  希爾排序

  希爾排序是將組分段,進行插入排序. 對想提高C#語言編程能力的朋友,我們可以互相探討一下。如:下面的程式,並

沒有實現多態,來,幫它實現一下。

using System;

namespace ShellSorter
{
public class ShellSorter
{
public void Sort(int [] list)
{
int inc;
for(inc=1;inc<=list.Length/9;inc=3*inc+1);
for(;inc>0;inc/=3)
{
for(int i=inc+1;i<=list.Length;i+=inc)
{
int t=list[i-1];
int j=i;
while((j>inc)&&(list[j-inc-1]>t))
{
list[j-1]=list[j-inc-1];
j-=inc;
}
list[j-1]=t;
}
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
ShellSorter sh=new ShellSorter();
sh.Sort(iArrary);
for(int m=0;m<iArrary.Length;m++)
Console.Write("{0} ",iArrary[m]);
Console.WriteLine();
}
}
}
 
*********************************************************************************
實際上任何元素,只要能夠互相比較就能夠進行排序了。比如你往裡面添加一堆字串,然後Sort,這樣也能夠排序,只是

排序的規則和字串的“大小比較”規則一致。  
   
  如果你需要對一堆的結構或者類進行排序,你只要實現IComparable這個介面就可以了。這個介面表示這個結構或者類的

執行個體能夠跟另外一個什麼東西比較出大小來。eg:  
  class   myRecord   :   IComparable  
  {  
          public   int   Num;  
          public   string   Description;  
   
          public   int   CompareTo(object   obj)  
          {  
                  myRecord   rec;  
                  rec   =   obj   as   myRecord;  
                  //   如果這個有待比較的東西是一個myRecord類  
                  if   (rec   !=   null)  
                  {  
                          return   Num.CompareTo(rec.Num);   //   大小取決於雙方的Num  
                  }  
                  else  
                  {  
                          return   -1;   //   否則總比這個東西小。  
                  }  
          }  
  }  
   
  這樣myRecord加入到ArrayList之後,就能夠按照Num的大小進行排序了。需要注意的是,ArrayList.Sort排序後是一個升

序結果,如果你需要降序的結果,只要把return的值取負值就行了,eg:   return   -Num.CompareTo(rec.Num);  
   
  另外關於那個return   -1需要做一些說明:  
  1、CompareTo的傳回值有如下含義:  
        >0     表示this   >   obj  
        ==0   表示this   ==   obj  
        <0     表示this   <   obj  
   
  2、CompareTo的參數obj不假定就是和this屬於同一個類型,因此必須自己進行判斷。這裡的else子句就是為了處理類型

不一樣的情況。  
   
  3、當類型不一樣的時候,也可以考慮throw   Exception,但是這樣也許會引起不必要的程式異常終止,所以我個人一般

採用return   -1;   的操作。那麼return   -1是什麼意思呢?由於Sort總是升序的,而一般情況下我們總是比較容易注意

到前面的東西,前面的東西一定是CompareTo小的,因此需要把myRecord的項目儘可能提到前面。為此,只要obj不是myRec

,就返回-1。  
   
  4、儘管從理論上來說,arr.Sort允許不同類型的資料之間進行排序,但是這樣做是不推薦的。因為你很難保重Sort到底

調用了myRecord對象的CompareTo還是obj對象的CompareTo。如果obj對象也總是希望自己處於最前面,就可能引起混亂。或

者obj總是在類型不一致的時候拋出異常,那麼程式還是會異常終止。因此,盡量不要在需要Sort的ArrayList混裝不同類型

的元素。  
   
  5、使用Sort的時候需要確保兩條:  
      A、這個ArrayList不能夠沒有任何元素,否則會引發異常(雖然這是一個MS的Bug,但是你不做Count   ==   0的檢

查,別人就會以為這是你的Bug)。  
      B、這個ArrayList裡面的每一個元素都實現了IComparable介面,或者你為Sort提供了實現IComparer介面的對象,否

則也會引發異常。所有系統預定義的類型都實現了IComparable介面,不需要擔心。至於IComparer,我建議不要管它,除非

你希望一會兒按照Num來排序,另一會兒按照Description來排序,再一會兒按照別的什麼東西來排序。所以這裡先不介紹了

,如果你有這個需求,告訴我。  
   
  6、不用懷疑效能方面的問題,一般情況下足夠快了。如果你發現效能方面的問題,先找找別的原因。至少,你應該想我

這樣寫成class   myRecord,而不是structure   myRecord。
*********************************************************************************
給你一個複雜的例子或許會給你提供一些更有意的思考.  
  using   System;  
  namespace   ExampleModel  
  {  
  public   class   BubbleSorter //冒泡排序  
  {  
  static   public   void   Sort(object[]   sortArray,CompareOp   rhsLhsGreater)  
  {  
  for(int   i=0;i<sortArray.Length;i++)  
  {  
  for(int   j=0;j<i;j++)  
  {  
  if(rhsLhsGreater(sortArray[i],sortArray[j]))  
  {  
  object   temp   =   sortArray[i];  
  sortArray[i]   =   sortArray[j];  
  sortArray[j]   =   temp;  
  }  
  }  
  }  
  }  
   
  }  
  public   class   Employee  
  {  
  private   string   name;  
  private   decimal   salary;  
   
  public   Employee(string   name,decimal   salary)                   {  
  this.name   =   name;  
  this.salary   =   salary;  
  //在此添加建構函式代碼  
  }  
  public   override   string   ToString()  
  {  
  return   string.Format(name   +   ",{0}",salary);  
  }  
  public   static   bool   RhsLhsGreater(object   lhs,object   rhs)  
  {  
  Employee   Lhs   =   (Employee)lhs; //類型轉換  
  Employee   Rhs   =   (Employee)rhs;  
  return   (Rhs.salary   >   Lhs.salary)?true:false;  
  }  
  //在此添加類代碼  
  }  
   
  public   delegate   bool   CompareOp(object   lhs,object   rhs); //委託  
   
  public   class   MainEntryPoint  
  {  
  public   static   void   Main(string[]   args)  
  {  
  Employee[]   employees   =  
  {  
  new   Employee("Karli   Watson",20000),  
  new   Employee("Bill   Gates",10000),  
  new   Employee("Peng   Song",25000),  
  new   Employee("Mortimer",(decimal)1000000.38),  
  new   Employee("Arabel   Jones",23000),  
  new   Employee("Avon   from   'Blak's'",50000)  
  };  
  CompareOp   EmployeeCompareOp   =   new   CompareOp(Employee.RhsLhsGreater);  
  BubbleSorter.Sort(employees,EmployeeCompareOp);  
  for(int   i=0;i<employees.Length;i++)  
  Console.WriteLine(employees[i].ToString());  
  //在此添加主程式  
  }  
  }    
  }  
  給程式可以對自訂的類進行排序!

*********************************************************************************

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.