自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

四種C#排序算法代碼示例

開發(fā) 后端 算法
這里將展示四種C#排序算法代碼示例,包括冒泡排序(Bubble)、選擇排序(Selection)、插入排序(InsertionSorter)等四種。
C#排序算法一般都涉及到循環(huán),以及賦值。通過排序,能進(jìn)行簡單的統(tǒng)計(jì)與分類,具有極其重要的價(jià)值。這里將介紹四種不同的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#委托實(shí)例簡單分析
  2. 一個(gè).NET委托的故事:彼得,老板和宇宙
  3. 解惑答疑:C#委托和事件
  4. 各版本.NET委托的寫法回顧
  5. 換一個(gè)角度看.NET中的理解委托和事件
責(zé)任編輯:彭凡 來源: CSDN
相關(guān)推薦

2024-10-24 08:04:00

2015-03-19 15:13:20

PHP基本排序算法代碼實(shí)現(xiàn)

2021-06-24 17:55:40

Python 開發(fā)編程語言

2009-08-20 09:52:31

C#參數(shù)類型

2009-08-26 15:04:35

C#轉(zhuǎn)換

2009-08-05 14:09:04

C#日期轉(zhuǎn)換

2009-08-26 18:14:11

C#排序算法

2009-09-22 14:20:39

C#播放聲音

2009-09-17 16:55:58

C#組件設(shè)計(jì)

2024-06-24 01:00:00

2017-04-25 08:26:39

神經(jīng)網(wǎng)絡(luò)解碼

2021-08-11 20:17:22

推薦算法系統(tǒng)

2024-06-18 08:37:25

場景異步編程代碼

2024-05-29 13:18:12

線程Thread?方式

2009-08-27 15:00:55

C#線程控制

2011-12-30 13:15:53

Java

2024-02-28 09:22:03

限流算法數(shù)量

2023-10-09 07:11:03

排序算法序列

2009-08-25 17:41:51

C#開發(fā)排序算法

2009-08-11 09:19:52

C#選擇排序C#算法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號