⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csharp.txt

📁 Csharp排序算法几种详细的算法冒泡选择希尔等并有自己做的注释
💻 TXT
字号:
一、冒泡排序(Bubble) 
二、选择排序
三、插入排序
四、希尔排序

一、冒泡排序(Bubble) 

using System; 

namespace BubbleSorter 
{ 
	public class BubbleSorter 
	{ 
	  public void Sort(int[] list) 
	  { 
	   int i,j,temp; 
	   bool done=false; 			/*done is a bool for judge  element*/
	   j=1; 				/*declare a var for a start number */
	   while((j<list.Length)&&(!done)) 	/*judge the condition which is meet the requirement  and begin a circulation */
	   { 					 
	      done=true; 			/*set "done=true" means the element in list[] has been Sort */
	      for(i=0;i<list.Length-j;i++) 	/*begin a circulation in order to sort the element in list[] */
		{ 
		  if(list[i]>list[i+1]) 	/*从第一个元素开始,每一个元素与下一个元素比较,如果前者大于后者,则前后交换值,最大的值在最后面  */
		  { 
		     done=false; 		/*修改done=false,表明排序尚未结束(前面定义done=true是为了最后一次结束循环的条件) */
		     temp=list[i]; 
		     list[i]=list[i+1]; 
		     list[i+1]=temp; 
		   }				 
		} 				
	     j++; 				/*J记录的是已经排好序的数字+1的值 */
	    } 
	   } 
	} 

	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(); 			/*实例化BubbleSorter */
	   sh.Sort(iArrary); 					/*调用实例方法 */
	   for(int m=0;m<iArrary.Length;m++) 
	   Console.Write("{0} ",iArrary[m]); 
	   Console.WriteLine(); 
	   } 
	 } 
} 

二、选择排序(Selection) 

using System; 

namespace SelectionSorter 
{ 
	public class SelectionSorter 		
	 { 
	    private int min; 						/* */
	    public void Sort(int [] list) 
	      { 
		for(int i=0;i<list.Length-1;i++) 			/*Start a circulation which includes list.Length steps */
		{ 
		  min=i; 
		  for(int j=i+1;j<list.Length;j++) 			
			/*Build a circulation which chooses the smallest number(list [min]) among (list.Length-i-1) elements */
		    { 
			if(list[j]<list[min]) 
			min=j; 
		    } 
		  int t=list[min]; 					/*set list[i] =list[min]  */
		  list[min]=list[i]; 				/*variable t is not an important variable,which can be replaced by 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(); 
	      } 
	  } 
} 

三、插入排序(InsertionSorter) 

using System; 

namespace InsertionSorter 
{ 
	public class InsertionSorter 
	  { 
	      public void Sort(int [] list) 				
	       { 
		for(int i=1;i<list.Length;i++) 			
		{ 
		 int t=list[i]; 			/*存放起点element的值 */
		 int j=i; 				/*记录下起点位置 */
		 while((j>0)&&(list[j-1]>t)) 
		 { 
		   list[j]=list[j-1]; 			/*连续两值比较,将较大值后移,直到 遇到比前者大的值后移(或已经移到最后)跳出循环 */
		   --j; 
		  } 
		list[j]=t; 				/*这样获得的便是相应元素t在list中的位置j */
	        } 
	       } 
	  } 

	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(); 
	       } 
	  } 
} 

四、希尔排序(ShellSorter) 

using System; 

namespace ShellSorter 
{ 
	public class ShellSorter 
	  { 
	     public void Sort(int [] list) 
	       { 
		 int inc=1; 					
		 for(;inc<=list.Length/9;inc=3*inc+1);			/* */
		 for(;inc>0;inc/=3) 
		 { 
		   for(int i=inc+1;i<=list.Length;i+=inc) 
		   { i
		    int t=list[i-1]; 
		    int j=i; 
		    while((j>inc)&&(list[j-inc-1]>t)) 		/*相隔inc个值的两值比较,将较大值后移,直到 遇到比前者大的值后移(或已经移到最后)跳出循环 */
		    { 
		      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(); 
		} 
	   } 
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -