惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - Bobby

SQL Top注意事项 asp.net回车提交表单 - Bobby - 博客园 C#关于值类型和引用类型 Asp.Net XML操作基类(修改,删除,新增,创建) 项目中的代码 客户端全部选中或取消Repeater中的CheckBox - Bobby - 博客园 javascript技巧参考(转载) js倒计时关闭窗口&计算字数 - Bobby - 博客园 CSS控制GridView固定表头 - Bobby - 博客园 js设置调用cookie设置背景颜色 字号、颜色 js简繁转换代码 ASP.NET错误处理的方式(总结) ASP.NET错误处理和程序优化 学习SQL应知道的动态SQL语句基本语法 js宝典学习笔记【转载】 在b/s开发中经常用到的javaScript技术整理 Visual C#常用函数和方法集汇总 SQL SERVER 与ACCESS、EXCEL的数据转换 - Bobby 不被浏览器屏蔽的对联广告
经典算法-C#四种排序算法
Bobby · 2007-04-28 · via 博客园 - Bobby

本文介绍了C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序

 冒泡排序

using System;

namespace BubbleSorter

{ public class BubbleSorter

{ public void Sort(int [] list)

{ int i,j,temp;

bool done=false;

j=1;

while((j<list.Length)&&(!done))

{ done=true;

for(i=0;i<list.Length-j;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;m<iArrary.Length;m++)

Console.Write("{0} ",iArrary[m]);

Console.WriteLine();

} }

}
 

选择排序

using System;


namespace SelectionSorter

{ public class SelectionSorter

{ private int min;

public void Sort(int [] list)

{ for(int i=0;i<list.Length-1;i++)

{ min=i;

for(int j=i+1;j<list.Length;j++)

{ if(list[j]<list[min])

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;m<iArrary.Length;m++)

Console.Write("{0} ",iArrary[m]);

Console.WriteLine();

} }

}
 

插入排序

using System;

namespace InsertionSorter

{ public class InsertionSorter

{ public void Sort(int [] list)

{ for(int i=1;i<list.Length;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;m<iArrary.Length;m++)

Console.Write("{0}",iArrary[m]);

Console.WriteLine();

} }

}
 

希尔排序

 希尔排序是将组分段,进行插入排序.

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;m<iArrary.Length;m++)

Console.Write("{0} ",iArrary[m]);

Console.WriteLine();

} }

}