Advertisement
Jakobhorak28

Untitled

Oct 2nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1.  
  2.         static void Main(string[] args)
  3.         {
  4.             int[] randarr = new int[100000000];
  5.             Random rnd = new Random(); ;
  6.             for (int i = 0; i < randarr.Length; i++)
  7.                 randarr[i] = rnd.Next(1,10000001);
  8.             Console.WriteLine("Ready");
  9.             Console.ReadLine();
  10.             Console.Write(Radix_Sort(randarr, 465));
  11.             Console.ReadLine();
  12.         }
  13.  
  14.         static double Radix_Sort(int[] arr, params int[] radix)
  15.         {
  16.             int minInt = arr.Min();
  17.             if (minInt < 0)
  18.                 for (int i = 0; i < arr.Length; i++)
  19.                     arr[i] = arr[i] - minInt;
  20.             int max = arr.Max();
  21.             if (radix.Length == 0)
  22.             {
  23.                 radix = new int[1];
  24.                 radix[0] = 0;
  25.             }
  26.             int p = 0;
  27.             System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  28.             sw.Start();
  29.             do
  30.             {
  31.                 Counting_Sort(arr, max, ref radix[0], p++);
  32.                 GC.Collect();
  33.                 GC.WaitForPendingFinalizers();
  34.             } while (GetPosValue(radix[0], p) <= max);
  35.             sw.Stop();
  36.             if (minInt < 0)
  37.                 for (int i = 0; i < arr.Length; i++)
  38.                    arr[i] = arr[i] + minInt;
  39.             return sw.ElapsedMilliseconds;
  40.         }
  41.  
  42.         static void Counting_Sort(int[] values, int maxvalue, ref int b, int pos)
  43.         {
  44.             int minvalue = 0;
  45.             if (b == 0)
  46.             {
  47.                 minvalue = values.Min();
  48.                 b = maxvalue - minvalue + 1;
  49.                 maxvalue -= minvalue;
  50.                 for (int i = 0; i < values.Length; i++)
  51.                     values[i] -= minvalue;
  52.                 pos = 0;
  53.             }
  54.             int[] c = new int[b];
  55.             for (int i = 0; i < b; i++)
  56.                 c[i] = 0;
  57.             int nextPos = GetPosValue(b, pos + 1);
  58.             pos = GetPosValue(b, pos);
  59.             for (int i = values.Length - 1; i >=0; i--)
  60.                 c[(values[i] % nextPos) / pos]++;
  61.             c[0]--;
  62.             for (int i = 1; i < b; i++)
  63.                 c[i] += c[i - 1];
  64.             int[] outarr = new int[values.Length];
  65.             for (int i = values.Length - 1; i >= 0; i--)
  66.             {
  67.                 outarr[c[(values[i] % nextPos) / pos]] = values[i] + minvalue;
  68.                 c[(values[i] % nextPos) / pos]--;
  69.             }
  70.             for (int i = values.Length - 1; i >= 0; i--)
  71.                 values[i] = outarr[i];
  72.             outarr = null;
  73.             c = null;
  74.         }
  75.  
  76.         static int GetPosValue(int sysBase, int posNum)
  77.         {
  78.             int value = 1;
  79.             for (int i = 0; i < posNum; i++)
  80.                 value *= sysBase;
  81.             return value;
  82.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement