I use this sample code to perform radix sort
for unsigned integer
array with N
size
inline int *indx( int *table){ int *ind=new int[65536]; int TMP=0; ind[0]=0; for(int i=1;i<65536;i++)ind[i]=TMP+=table[i-1]; return (ind); } void m_radix(unsigned int *array,const int N){ int *count=new int [65536](); int *lpos=new int[65536]; // hold the last position int *fpos; // hold the first position for(int i=0;i<N;i++) count[array[i]&0xffff]++; fpos=indx(count); for(int i=0;i<65536;i++) lpos[i]=fpos[i]+count[i]; for(int j=0;j<65536;j++) for(int i=fpos[j];i<lpos[j];i++) while((array[i]&0xffff)!=j) std::swap(array[i],array[fpos[array[i]&0xffff]++]); count=new int [65536](); for(int i=0;i<N;i++) count[array[i]>>16]++; fpos=indx(count); for(int i=0;i<65536;i++) lpos[i]=fpos[i]+count[i]; for(int j=0;j<65536;j++) for(int i=fpos[j];i<lpos[j];i++) while((array[i]>>16)!=j) std::swap(array[i],array[fpos[array[i]>>16]++]); }
I think that this algorithm is linear however benchmarking show low performence against algorithm using auxilary array. where is the problem so ?