三种常用C#排序算法

开发 后端 算法
在我们的.NET项目中,经常会遇到通过C#排序算法来解决的问题,下面列出常用的三种C#排序算法,希望可以对您有所帮助。

C#算法是C#语言学习中的重要一环,C#排序算法无论在语言基础还是数据结构中都是必不可少的知识,下面我们来看著名的冒泡排序、选择排序和插入排序,这是最常用的三种C#排序算法。

C#排序算法之冒泡排序  
   
一下是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#排序算法之选择排序    

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#排序算法之插入排序    
   
C#插入排序算法主要对应项目需求中的结果集插入,文本插入和数据库插入等。下面的程序通过多态性来实现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. }    

 

【编辑推荐】

  1. C#算法解决的一道面试题
  2. C#算法实现字符串反转浅析
  3. C#算法之约瑟夫环算法浅析
  4. C#算法之选择排序浅析
  5. C#算法巧解八皇后问题浅析
责任编辑:佚名 来源: 百度空间
相关推荐

2009-08-26 18:10:44

C# using的用法

2009-08-06 15:26:18

C#异常类型

2009-09-08 17:20:01

C#排序算法

2009-08-26 17:21:05

C# using

2009-08-27 16:53:05

C# using作用

2009-09-08 10:37:57

C#遍历CheckBo

2024-02-26 13:47:00

C#Socket数据接收

2023-10-09 07:11:03

排序算法序列

2009-07-30 16:27:33

C#比较时间

2009-08-25 17:41:51

C#开发排序算法

2009-08-11 09:19:52

C#选择排序C#算法

2021-02-01 10:17:14

编程C语言计算机

2009-09-14 18:11:23

C#排序方法

2010-11-22 17:00:10

MySQL建表语句

2011-07-04 15:14:49

C#

2009-08-20 17:30:02

C#连接字符串

2009-08-03 17:38:12

排序算法C#数据结构

2023-08-02 10:10:00

C#C++

2009-11-11 17:40:33

路由器协议

2010-05-11 14:08:50

MySQL数字类型
点赞
收藏

51CTO技术栈公众号