Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- int[] randarr = new int[100000000];
- Random rnd = new Random(); ;
- for (int i = 0; i < randarr.Length; i++)
- randarr[i] = rnd.Next(1,10000001);
- Console.WriteLine("Ready");
- Console.ReadLine();
- Console.Write(Radix_Sort(randarr, 465));
- Console.ReadLine();
- }
- static double Radix_Sort(int[] arr, params int[] radix)
- {
- int minInt = arr.Min();
- if (minInt < 0)
- for (int i = 0; i < arr.Length; i++)
- arr[i] = arr[i] - minInt;
- int max = arr.Max();
- if (radix.Length == 0)
- {
- radix = new int[1];
- radix[0] = 0;
- }
- int p = 0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- do
- {
- Counting_Sort(arr, max, ref radix[0], p++);
- GC.Collect();
- GC.WaitForPendingFinalizers();
- } while (GetPosValue(radix[0], p) <= max);
- sw.Stop();
- if (minInt < 0)
- for (int i = 0; i < arr.Length; i++)
- arr[i] = arr[i] + minInt;
- return sw.ElapsedMilliseconds;
- }
- static void Counting_Sort(int[] values, int maxvalue, ref int b, int pos)
- {
- int minvalue = 0;
- if (b == 0)
- {
- minvalue = values.Min();
- b = maxvalue - minvalue + 1;
- maxvalue -= minvalue;
- for (int i = 0; i < values.Length; i++)
- values[i] -= minvalue;
- pos = 0;
- }
- int[] c = new int[b];
- for (int i = 0; i < b; i++)
- c[i] = 0;
- int nextPos = GetPosValue(b, pos + 1);
- pos = GetPosValue(b, pos);
- for (int i = values.Length - 1; i >=0; i--)
- c[(values[i] % nextPos) / pos]++;
- c[0]--;
- for (int i = 1; i < b; i++)
- c[i] += c[i - 1];
- int[] outarr = new int[values.Length];
- for (int i = values.Length - 1; i >= 0; i--)
- {
- outarr[c[(values[i] % nextPos) / pos]] = values[i] + minvalue;
- c[(values[i] % nextPos) / pos]--;
- }
- for (int i = values.Length - 1; i >= 0; i--)
- values[i] = outarr[i];
- outarr = null;
- c = null;
- }
- static int GetPosValue(int sysBase, int posNum)
- {
- int value = 1;
- for (int i = 0; i < posNum; i++)
- value *= sysBase;
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement