四种C#排序算法代码示例

C#排序算法一般都涉及到循环,以及赋值。通过排序,能进行简单的统计与分类,具有极其重要的价值。这里将介绍四种不同的C#排序算法代码,希望对大家有所帮助。

C#排序算法一、冒泡排序(Bubble)

 
 
 
  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{   
  12. done=true;   
  13. for(i=0;i{   
  14. if(list[i]>list[i+1])   
  15. {   
  16. done=false;   
  17. temp=list[i];   
  18. list[i]=list[i+1];   
  19. list[i+1]=temp;   
  20. }   
  21. }   
  22. j++;   
  23. }   
  24. }   
  25. }   
  26.  
  27. public class MainClass   
  28. {   
  29. public static void Main()   
  30. {   
  31. int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};   
  32. BubbleSorter sh=new BubbleSorter();   
  33. sh.Sort(iArrary);   
  34. for(int m=0;mConsole.Write("{0} ",iArrary[m]);   
  35. Console.WriteLine();   
  36. }   
  37. }   
  38. }  

C#排序算法二、选择排序(Selection)

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

C#排序算法三、插入排序(InsertionSorter)

 
 
 
  1. using System;   
  2. namespace InsertionSorter   
  3. {   
  4. public class InsertionSorter   
  5. {   
  6. public void Sort(int [] list)   
  7. {   
  8. for(int i=1;i{   
  9. int t=list[i];   
  10. int j=i;   
  11. while((j>0)&&(list[j-1]>t))   
  12. {   
  13. list[j]=list[j-1];   
  14. --j;   
  15. }   
  16. list[j]=t;   
  17. }   
  18. }   
  19. }   
  20.  
  21. public class MainClass   
  22. {   
  23. public static void Main()   
  24. {   
  25. int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};   
  26. InsertionSorter ii=new InsertionSorter();   
  27. ii.Sort(iArrary);   
  28. for(int m=0;mConsole.Write("{0}",iArrary[m]);   
  29. Console.WriteLine();   
  30. }   
  31. }   
  32. }  

C#排序算法四、希尔排序(ShellSorter)

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

【编辑推荐】

  1. C#委托实例简单分析
  2. 一个.NET委托的故事:彼得,老板和宇宙
  3. 解惑答疑:C#委托和事件
  4. 各版本.NET委托的写法回顾
  5. 换一个角度看.NET中的理解委托和事件
THE END