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

开发 后端 算法
这里将展示四种C#排序算法代码示例,包括冒泡排序(Bubble)、选择排序(Selection)、插入排序(InsertionSorter)等四种。
C#排序算法一般都涉及到循环,以及赋值。通过排序,能进行简单的统计与分类,具有极其重要的价值。这里将介绍四种不同的C#排序算法代码,希望对大家有所帮助。

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

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

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

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

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

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

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

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

【编辑推荐】

  1. C#委托实例简单分析
  2. 一个.NET委托的故事:彼得,老板和宇宙
  3. 解惑答疑:C#委托和事件
  4. 各版本.NET委托的写法回顾
  5. 换一个角度看.NET中的理解委托和事件
责任编辑:彭凡 来源: CSDN
相关推荐

2024-10-24 08:04:00

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#组件设计

2024-06-24 01:00:00

2017-04-25 08:26:39

神经网络解码

2021-08-11 20:17:22

推荐算法系统

2024-06-18 08:37:25

场景异步编程代码

2024-05-29 13:18:12

线程Thread​方式

2009-08-27 15:00:55

C#线程控制

2024-02-28 09:22:03

限流算法数量

2011-12-30 13:15:53

Java

2023-10-09 07:11:03

排序算法序列

2009-08-11 09:19:52

C#选择排序C#算法

2009-08-25 17:41:51

C#开发排序算法
点赞
收藏

51CTO技术栈公众号