C# 排序算法

C#排序算法之冒泡排序    

 

这是最原始,也是众所周知的最慢的算法了。他的名字的由来因为它的工作看来象是冒泡

 
  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. }     
  31. public   class   MainClass     
  32. {       
  33. public   static   void   Main()     
  34. {     
  35. int[]   iArrary=new   int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};     
  36. BubbleSorter   sh=new   BubbleSorter();     
  37. sh.Sort(iArrary);     
  38. forint   m=0;m<iArrary.Length;m++)     
  39. Console.Write("{0}   ",iArrary[m]);       
  40. Console.WriteLine();     
  41. }     
  42. }     
  43. }    

C#排序算法之选择排序    

选择法,这种方法提高了一点性能(某些情况下) 这种方法类似我们人为的排序习惯:从数据中选择最小的同第一个值交换,在从省下的部分中 选择最小的与第二个交换,这样往复下去。

 
  1. using   System;     
  2.       
  3.       
  4. namespace   SelectionSorter     
  5. {     
  6. public   class   SelectionSorter     
  7. {       
  8. private   int   min;     
  9. public   void   Sort(int   []   list)     
  10. {     
  11. forint   i=0;i<list.Length-1;i++)     
  12. {     
  13. min=i;     
  14. forint   j=i+1;j<list.Length;j++)     
  15. {     
  16. if(list[j]<list[min])     
  17. min=j;     
  18. }     
  19. int   t=list[min];     
  20. list[min]=list[i];     
  21. list[i]=t;     
  22. }     
  23.       
  24.       
  25. }     
  26. }     
  27. public   class   MainClass     
  28. {       
  29. public   static   void   Main()     
  30. {     
  31. int[]   iArrary=new   int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};     
  32. SelectionSorter   ss=new   SelectionSorter();     
  33. ss.Sort(iArrary);     
  34. forint   m=0;m<iArrary.Length;m++)     
  35. Console.Write("{0}   ",iArrary[m]);       
  36. Console.WriteLine();     
  37.       
  38.       
  39. }     
  40. }     
  41. }    

C#排序算法之插入排序    

插入法较为复杂,它的基本工作原理是抽出牌,在前面的牌中寻找相应的位置插入,然后继续下一张

  1. using   System;     
  2.       
  3.       
  4. namespace   InsertionSorter     
  5. {     
  6. public   class   InsertionSorter     
  7. {     
  8. public   void   Sort(int   []   list)     
  9. {     
  10. forint   i=1;i<list.Length;i++)     
  11. {     
  12. int   t=list[i];     
  13. int   j=i;     
  14. while((j>0)&&(list[j-1]>t))     
  15. {     
  16. list[j]=list[j-1];     
  17. --j;     
  18. }     
  19. list[j]=t;     
  20. }     
  21.       
  22.       
  23. }     
  24. }     
  25. public   class   MainClass     
  26. {       
  27. public   static   void   Main()     
  28. {     
  29. int[]   iArrary=new   int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};     
  30. InsertionSorter   ii=new   InsertionSorter();     
  31. ii.Sort(iArrary);     
  32. forint   m=0;m<iArrary.Length;m++)     
  33. Console.Write("{0}",iArrary[m]);       
  34. Console.WriteLine();     
  35. }     
  36. }     
  37. }    
C#排序算法之交换排序: 
交换法的程序最清晰简单,每次用当前的元素一一的同其后的元素比较并交换。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值