RADIX SORT
namespace RadixSort
{
class Program
{
static void ByteBasedSort(int ByteNo, int[] src, int[] dest)
{
int[] count = new int[256];
int[] index = new int[256];
for (int i = 0; i < src.Length; i++)
{
count[(src[i] >> (ByteNo * 8)) & 0xff]++;
}
index[0] = 0;
for (int i = 1; i < 256; i++)
{
index[i] = index[i - 1] + count[i - 1];
}
for (int i = 0; i < src.Length; i++)
{
dest[index[(src[i] >> (ByteNo * 8)) & 0xff]] = src[i];
index[(src[i] >> (ByteNo * 8)) & 0xff]++;
}
}
static void RadixSort(int[] array)
{
int[] temp = new int[array.Length];
ByteBasedSort(0, array, temp);
ByteBasedSort(1, temp, array);
ByteBasedSort(2, array, temp);
ByteBasedSort(3, temp, array);
}
static void Main(string[] args)
{
int[] arr = { 24, 56, 24, 85, 90 };
RadixSort(arr);
foreach (int a in arr)
Console.WriteLine(a);
}
}
}
No comments:
Post a Comment