WEB开发网
开发学院软件开发C语言 C#实现所有经典排序算法汇总 阅读

C#实现所有经典排序算法汇总

 2009-05-06 08:27:04 来源:WEB开发网   
核心提示: 8、计数排序计数排序//计数排序 /// <summary> /// counting sort /// </summary> /// <param name="arrayA">input array</param> //

8、计数排序

计数排序
//计数排序
        /// <summary>
        /// counting sort
        /// </summary>
        /// <param name="arrayA">input array</param>
        /// <param name="arrange">the value arrange in input array</param>
        /// <returns></returns>
        public int[] CountingSort(int[] arrayA, int arrange)
        {   
            //array to store the sorted result, 
            //size is the same with input array.
            int[] arrayResult = new int[arrayA.Length];   
            //array to store the direct value in sorting process  
            //include index 0;   
            //size is arrange+1;   
            int[] arrayTemp = new int[arrange+1];   
            //clear up the temp array   
            for(int i = 0; i <= arrange; i++)   
            {       
                arrayTemp[i] = 0; 
            }   
            //now temp array stores the count of value equal 
            for(int j = 0; j < arrayA.Length; j++)  
            {      
                arrayTemp[arrayA[j]] += 1;  
            }   
            //now temp array stores the count of value lower and equal 
            for(int k = 1; k <= arrange; k++)  
            {      
                arrayTemp[k] += arrayTemp[k - 1]; 
            }    
            //output the value to result   
            for (int m = arrayA.Length-1; m >= 0; m--)  
            {       
                arrayResult[arrayTemp[arrayA[m]] - 1] = arrayA[m];   
                arrayTemp[arrayA[m]] -= 1; 
            }   
            return arrayResult;
        }

上一页  1 2 3 4 5 6 7  下一页

Tags:实现 所有 经典

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接