冒泡排序 C#
?主程序入口
class?Program????{
????????static?void?Main(string[]?args)
????????{
????????????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.ReadLine();
????????}
? ? }?
冒泡排序方法
?
?1??class?BubbleSorter?2?????{
?3?????????///?<summary>
?4?????????///?冒泡排序
?5?????????///?</summary>
?6?????????public?void?Sort(int[]?list)
?7?????????{
?8?????????????int?i,?j,?temp;
?9?????????????bool?done?=?false;
10?????????????j?=?1;
11?????????????while?((j?<?list.Length)?&&?(!done))//判斷長度
12?????????????{
13?????????????????done?=?true;
14?????????????????for?(i?=?0;?i?<?list.Length?-?j;?i++)
15?????????????????{
16?????????????????????if?(list[i]?>?list[i?+?1])
17?????????????????????{
18?????????????????????????done?=?false;
19?????????????????????????temp?=?list[i];
20?????????????????????????list[i]?=?list[i?+?1];//交換數據
21?????????????????????????list[i?+?1]?=?temp;
22?????????????????????}
23?????????????????}
24?????????????????j++;
25?????????????}
26?????????}
27?????}?
轉載于:https://www.cnblogs.com/Yellowshorts/archive/2013/05/17/3083596.html
總結
- 上一篇: Openssl的证书格式转换
- 下一篇: C#基于Socket的简单聊天室实践