基数排序
//对于数字
#include<iostream>
#include<string>
#include<vector>
using namespace std;
#define MAX 8
#define RADIX 10
#define MAX_SPACE 10000
struct Node
{
string data;
int next;
Node():next(0){};
};
struct list
{
Node r[MAX_SPACE];
int keynum;
int recnum;
list():keynum(0),recnum(0){};
};
void Distribute( list & L, int i, vector<int> & f, vector<int> & e )
{
for( int j = 0; j < RADIX; j++ )
{
f[j]=0;
e[j]=0;
}
int tmp;
for( int p = L.r[0].next; p; p = L.r[p].next )
{
int len =L.r[p].data.size();
if(len >= i )
{
tmp = L.r[p].data[len-i] -'0';
}
else
{
tmp = 0;
}
if( !f[tmp])
{
f[tmp]=p;
}
else
{
L.r[e[tmp]].next = p;
}
e[tmp]=p;
}
}
void Collect(list &L, int i,vector<int> &f,vector<int> &e )
{
int j;
int tmp;
for( j = 0; !f[j]; j++ );
L.r[0].next = f[j];
tmp=e[j];
while( j < RADIX )
{
for( ++j; j < RADIX-1 && !f[j]; ++j );
if(f[j])
{
//cout<<f[j]<<endl;
L.r[tmp].next = f[j];
tmp = e[j];
}
if( j == RADIX-1 )
break;
}
L.r[tmp].next = 0;
}
void RadixSort( list &L )
{
vector<int >f;
vector<int>e;
for( int j = 0; j < RADIX; j++ )
{
f.push_back(0);
e.push_back(0);
}
int i;
for( i = 0; i < L.recnum; ++i )
{
L.r[i].next = i+1;
}
L.r[L.recnum].next=0;
for( int k= 1; k < L.keynum + 1 ; ++k )
{
Distribute(L,k,f,e);
Collect(L,k,f,e);
}
}
int main()
{
list L;
string ctmp;
if( ! freopen("text.txt","r",stdin) )
{
cout<<"file fail !"<<endl;
exit(0);
};
L.keynum=3;
while(! cin.eof() )
{
cin>>ctmp;
L.r[L.recnum+1].data=ctmp;
L.recnum++;
}
RadixSort(L);
for( int p = L.r[0].next; p; p = L.r[p].next )
{
cout<<L.r[p].data<<endl;
}
system("pause");
return 0;
}