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

📄 countsort.cs

📁 counting sort algorithm in visual studio
💻 CS
字号:

/***************************************************************************/
//Count Sort.cs 
//Description :-
//Program to implement  Count Sort ( Sorting Alogorithm Complexity = Linear Time )
//Coder :- Razi Bin Rais
/*****************************************************************************/

using  System;
class CountSort
{
	int []thearray;
	int i;

	public CountSort(int size)
	{
	
		thearray = new int [size] ;
		Random ran = new Random();
			
			
		for(int i = 0 ; i< size; i++)
		{
			
			thearray[i] = ran.Next(i,i*size);
			

		}

			
	}
	// Find maximum  Number in the Array 
	// This will take O(n) time
	private  int Max()
	{
		int max = thearray[0];	
		for(i = 1 ; i<thearray.Length ;i++)
			
		{
			if (thearray[i] >  max ) {  max = thearray[i] ; } 
				
		}
				
		return max;	
		
		
	}


	public void Count_Sort()
	{

			
		try 
		{ 
			
			int k = Max();
			
			//k = k+1;
			//Console.WriteLine(k.ToString());
			// holds the sorted output
		        
			int []output = new int[thearray.Length]; 
		        
			// provides temperarory storage , this is of size equal 
			//to max no in input thearray ;
		        
			int []temp = new int[k+1]; 

		   	 
		   	
			for( i = 0 ; i<k+1 ; i++)
			{
		   	
				temp[ i ] = 0;
			}
		   
			for(i = 0 ; i<thearray.Length  ; i++)
			{
		   	
				temp [ thearray [ i ] ] = temp [ thearray [ i ] ] + 1;
		   	  
			}
		   	  
		   	  
			for( i = 1 ; i<k+1 ; i++)
			{
		   	
				temp[ i ] = temp[ i ] + temp[ i - 1]; 
 		   		
		   		 
			}  
		   
			for(i = thearray.Length-1  ;  i>=0 ; i--)
			{
		   	
				output [ temp [ thearray [ i ] ] -1  ] = thearray [ i ] ;
				temp [ thearray [ i ]  ]  = temp [ thearray [ i ]  ]  - 1;
		   	 
		   	
			}
		   	  
		    
			for(i = 0 ; i<thearray.Length  ; i++)
			{
				// Console.WriteLine( output[i].ToString());		   	
				thearray[  i ]  = output [  i ] ;
			
			}	   	  
		   	 
		}//end try
	
		catch ( System.Exception e)
		{
		   	
			Console.WriteLine(e.ToString());
			Console.Read();	
		   	
		}
		  
		   
	}
		   
		   
	public void display()
	{

		Console.WriteLine("The Array is ");
			
		for ( i = 0 ; i< thearray.Length ; i++) 
		{ 
			
			Console.WriteLine(thearray[i].ToString());
			
		}	
			
		Console.WriteLine("-------------------\n");
		 
	}
 
}//end of class 


class CountSortApp
{


	public static void Main(string  []args) 
	{
		
		int  size = 10 ; 
		
		CountSort countSort = new CountSort(size);
		
		countSort.display();
		
		countSort.Count_Sort();
		 
		countSort.display();
		
		Console.Read();	
		 
	
	
	 
	}	


}

⌨️ 快捷键说明

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