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

开发 后端 算法
这里将展示四种C#排序算法代码示例,包括冒泡排序(Bubble)、选择排序(Selection)、插入排序(InsertionSorter)等四种。
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中的理解委托和事件
责任编辑:彭凡 来源: CSDN
相关推荐

2015-03-19 15:13:20

PHP基本排序算法代码实现

2021-06-24 17:55:40

Python 开发编程语言

2009-08-20 09:52:31

C#参数类型

2009-08-26 15:04:35

C#转换

2009-08-05 14:09:04

C#日期转换

2009-08-26 18:14:11

C#排序算法

2009-09-22 14:20:39

C#播放声音

2009-09-17 16:55:58

C#组件设计

2017-04-25 08:26:39

神经网络解码

2021-08-11 20:17:22

推荐算法系统

2011-12-30 13:15:53

Java

2024-02-28 09:22:03

限流算法数量

2023-10-09 07:11:03

排序算法序列

2009-08-27 15:00:55

C#线程控制

2009-08-25 17:41:51

C#开发排序算法

2009-08-11 09:19:52

C#选择排序C#算法

2010-05-25 08:34:10

C# 4.0

2023-11-15 07:40:40

2009-08-03 17:38:12

排序算法C#数据结构

2023-07-11 10:24:00

分布式限流算法
点赞
收藏

51CTO技术栈公众号