C#开发排序算法实例讲解

来源:岁月联盟 编辑:zhu 时间:2009-08-26

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. 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. for(int m=0;m<iArrary.Length;m++)  
  38. Console.Write("{0} ",iArrary[m]);  
  39. Console.WriteLine();  
  40. }  
  41. }  
  42. }  

选择排序

本人用了C#开发排序算法。希望能为C#语言的学习者带来一些益处。不要忘了,学语言要花大力气学数据结构和算法。

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

插入排序

插入排序算法。对想提高C#语言编程能力的朋友,我们可以互相探讨一下。如:下面的程序,并没有实现多态,来,帮它实现一下。

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

希尔排序

希尔排序是将组分段,进行插入排序. 对想提高C#语言编程能力的朋友,我们可以互相探讨一下。如:下面的程序,并没有实现多态,来,帮它实现一下。

  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. 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. ShellSorter sh=new ShellSorter();  
  33. sh.Sort(iArrary);  
  34. for(int m=0;m<iArrary.Length;m++)  
  35. Console.Write("{0} ",iArrary[m]);  
  36. Console.WriteLine();  
  37. }  
  38. }  
  39. }  

以上介绍C#开发排序算法