ctlove0523 于 2014.10.27 16:07 提问

#include
#include
#include
using namespace std;
void counting_sort(vector& A,vector& B,int k);
void radix_sort(vector& A,vector& B,int d,int k);
int seek_number(int A,int k);
int seek_number(int A,int k)
{
//假设int类型能表示的最大数为32767(五位数)
switch(k)
{
case 1: return(A/10);
break;
case 2: return((A%100)/10);
break;
case 3: return((A%1000)/100);
break;
case 4: return((A%10000)/1000);
break;
case 5: return(A/10000);
break;
default :cout<<"你的输入不正确！";

``````}
``````

}

``````void raidx_sort(vector<int>& A,vector<int>& B,int d,int k)
``````

{
//创建临时Vctor,k的取值范围只能是[0,9]
vector C;
vectorD(A.size());
for(int i=0;i C.push_back(0);
}
//按照从低位到高位的顺序进行排序
//定义一个迭代器用于便利vector中的元素
vector::iterator pos;
int count;
for( count=1;count<=d;++count)
{
//去除A中的第i位数字并存储在D中
for(pos=A.begin();pos!=A.end();++pos){
D.push_back(seek_number(*pos,count));
}
for(pos=A.begin()+1;pos!=A.end();++pos){
D[*pos]+=1;
}
for(int i=1;i D[i]+=D[i-1];
}
for(pos=A.end();pos!=(A.begin()+1);--pos){
B[C[*pos]]=*pos;
--C[*pos];
}
A=B;
}
}
int main()
{
vector coll;
vector::iterator pos1;
coll.push_back(0);
coll.push_back(329);
coll.push_back(457);
coll.push_back(657);
coll.push_back(839);
coll.push_back(436);
coll.push_back(720);
coll.push_back(355);
cout<<"Befor sort:";
for(pos1=coll.begin();pos1!=coll.end();++pos1){
cout<<*pos1<" ";
}
cout< vector boll(coll.size());