• Top Posts

    C# program to Distribution Sort


     using System;  
     using System.Collections.Generic;  
     using System.Linq;  
     using System.Text;  
     using System.Threading.Tasks;  
     public class Main  
     {  
       /*    Distribution sort    */  
       static public void Dist_Sort(int[] array)  
       {  
         int cnt = array.Length;  
         int min = array.Min();  
         int max = array.Max();  
         int range = max - min + 1;  
         var dict = new Dictionary<int, int>(range);  
         for (int ii = 0; ii < cnt; ++ii)  
         {  
           var elem = array[ii];  
           int count;  
           if (!dict.TryGetValue(elem, out count))  
             count = 0;  
           ++count;  
           dict[elem] = count;  
         }  
         int position = 0;  
         for (int ii = 0; ii < range; ++ii)  
         {  
           int count;  
           if (!dict.TryGetValue(ii, out count))  
             continue;  
           for (int jj = 0; jj < count; ++jj)  
           {  
             array[position] = ii;  
             ++position;  
           }  
         }  
       }  
     }  
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728