Went back and looked at the radix sort and found that it when sorting say 10,000,000 3 digit numbers it can use almost a gig and a half of ram. To try and fix this I changed the ArrayLists to Queues so that an item is only moved to another queue not copied to another queue, I also stopped copying the items to sort into a new variable at the start of the function it was a waste. The old Radix sort used a gig to a gig and a half of ram on average the new version uses 600mb to 800mb on average. Both run in linear time with similar performance.
Not really any point to this yet just seeing if I could make it better. There’s still probably a million improvements to make but this is what I got so far.
Provided in c#/mono once again.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace RadixSort
{
class Program
{
static void Main(string[] args)
{
System.Random rand = new Random(System.DateTime.Now.Millisecond);
int items = Convert.ToInt32(args[0]);
Queue Items = new Queue();
int c = 0;
while (c <= items)
{
Items.Enqueue(rand.Next(0, 999));
c++;
}
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
Items = RadixSort(Items, 3);
sw.Stop();
Console.WriteLine("Sorted in :: " + sw.Elapsed.ToString());
}
static Queue RadixSort(Queue Items, int Digits)
{
int Digit = Digits - 1;
while (Digit >= 0)
{
Queue Zero = new Queue();
Queue One = new Queue();
Queue Two = new Queue();
Queue Three = new Queue();
Queue Four = new Queue();
Queue Five = new Queue();
Queue Six = new Queue();
Queue Seven = new Queue();
Queue Eight = new Queue();
Queue Nine = new Queue();
int UpperLimit = Items.Count;
int counter = 1;
while (counter < UpperLimit)
{
int i = Convert.ToInt32(Items.Dequeue());
counter++;
if (i.ToString().PadLeft(Digits, '0')[Digit] == '0')
{
Zero.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '1')
{
One.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '2')
{
Two.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '3')
{
Three.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '4')
{
Four.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '5')
{
Five.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '6')
{
Six.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '7')
{
Seven.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '8')
{
Eight.Enqueue(i);
continue;
}
if (i.ToString().PadLeft(Digits, '0')[Digit] == '9')
{
Nine.Enqueue(i);
continue;
}
}
Items = new Queue();
foreach (int i in Zero)
{
Items.Enqueue(i);
}
foreach (int i in One)
{
Items.Enqueue(i);
}
foreach (int i in Two)
{
Items.Enqueue(i);
}
foreach (int i in Three)
{
Items.Enqueue(i);
}
foreach (int i in Four)
{
Items.Enqueue(i);
}
foreach (int i in Five)
{
Items.Enqueue(i);
}
foreach (int i in Six)
{
Items.Enqueue(i);
}
foreach (int i in Seven)
{
Items.Enqueue(i);
}
foreach (int i in Eight)
{
Items.Enqueue(i);
}
foreach (int i in Nine)
{
Items.Enqueue(i);
}
Digit--;
}
return Items;
}
}
}