R0b0t1
2008-11-05 20:49:42
Yeah, it is supposedly the best (and so far proven the best), although it takes some work to get it to sort negatives (which mine does not). It will, however, also sort doubles and floats (I think Delphi only has doubles, IIRC). Note this is a pretty… Er… Badly coded method…
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define ARRAY_SIZE 5
#define radixsort_f(source, temp, N) radix_32(0, 4, N, (int *)source, (int *)temp);
#define radixsort_d(source, temp, N) radix_64(0, 8, N, (uint64_t *)source, (uint64_t *)temp);
void radix_32(int byte, int bytemax, int N, int *source, int *dest)
{
int count[256], index[256], i;
memset(count, 0, sizeof (count));
if(bytemax % 2 != 0) bytemax++;
for(i = 0; i < N; i++)
count[((source[i]) >> (byte * 8)) & 0xFF]++;
index[0] = 0;
for(i = 1; i < 256; i++)
index[i] = index[i - 1] + count[i - 1];
for(i = 0; i < N; i++)
dest[index[((source[i]) >> (byte * 8)) & 0xFF]++] = source[i];
if(byte + 1 <= bytemax) radix_32(byte + 1, bytemax, N, dest, source);
else return;
}
void radix_64(int byte, int bytemax, int N, uint64_t *source, uint64_t *dest)
{
int count[256], index[256], i;
memset(count, 0, sizeof (count));
if(bytemax % 2 != 0) bytemax++;
for(i = 0; i < N; i++)
count[((source[i]) >> (byte * 8)) & 0xFF]++;
index[0] = 0;
for(i = 1; i < 256; i++)
index[i] = index[i - 1] + count[i - 1];
for(i = 0; i < N; i++)
dest[index[((source[i]) >> (byte * 8)) & 0xFF]++] = source[i];
if(byte + 1 <= bytemax) radix_64(byte + 1, bytemax, N, dest, source);
else return;
}
void make_random(int *data, int N)
{
int i;
for(i = 0; i < N; i++) data[i] = abs(rand());
}
int main(void)
{
double data[ARRAY_SIZE] = {0, 2.123, .1337, 100, .42};
double temp[ARRAY_SIZE];
int i;
//make_random((int *)data, ARRAY_SIZE);
radixsort_d(data, temp, ARRAY_SIZE);
for(i = 0; i < ARRAY_SIZE; i++) printf("%f\n", data[i]);
return 0;
}