#include <stdio.h> #include <math.h> #include <malloc.h> typedef int ElementType; #define DIGIT 31 #define RADIX_10 10 int GetNumInPos(ElementType Num,int Pos) { ElementType Temp = pow(10,Pos-1); return ((Num/Temp) % 10); } void RadixSort(ElementType *Array,int ArrayLen) { int i; int Position; int ArrayEnd; int CurrentIndex; int RadixArrayEnd; ElementType *RadixArray[RADIX_10]; int RadixArraySum[RADIX_10]; for(i = 0;i < RADIX_10;i ++) { RadixArray[i] = malloc((ArrayLen+1)*sizeof(ElementType)); RadixArraySum[i] = 0; } for(Position = 1;Position <= DIGIT;Position ++) { //distribute for(i = 0;i < ArrayLen;i ++) { CurrentIndex = GetNumInPos(Array[i],Position); RadixArrayEnd = ++ RadixArraySum[CurrentIndex]; RadixArray[CurrentIndex][RadixArrayEnd] = Array[i]; } //collect for(i = 0,ArrayEnd = 0;i < RADIX_10;i ++) { for(RadixArrayEnd = 1;RadixArrayEnd <= RadixArraySum[i];RadixArrayEnd ++) { Array[ArrayEnd++] = RadixArray[i][RadixArrayEnd]; } RadixArraySum[i] = 0; } } } int main() { ElementType TestArray[10] = {18,224,3332,4443,55,31,66,79,90,7}; RadixSort(TestArray,10); int i; for(i = 0;i < 10;i ++) { printf("%d ",TestArray[i]); } printf(" "); return 0; }