Radix sort is effective when the size of elements as MAX_SIZE are short. and it’s steady.
complexity:MAX_SIZE*length(array)
#include<bits/stdc++.h>
using namespace std;
int L[] = {3321,1,10,9680,577,9420,7,5622,4793,2030,3138,82,2599,743,4127};
int cot[15];
int MAX = 4; //the max length of elements
int base = 1;
int len;
int tmp[100][100];
int get_digit(int x){
return x/base%10;
}
void RadixSort(int n){
int i,j;
int res;
for(i=0;i<MAX;i++){
for(j=0;j<n;j++){
res = get_digit(L[j]);
tmp[res][cot[res]++] = L[j];
//cot[ress] maintain the length the digit is res
}
int k = 0;
for(j=0;j<10;j++){
for(int jj=0;jj<cot[j];jj++){
L[k++] = tmp[j][jj];
}
}
memset(cot,0,sizeof(cot));
base*=10;
}
}
int main(){
len = sizeof(L)/sizeof(int);
memset(cot,0,sizeof(cot));
RadixSort(len);
for(int i = 0;i<len;i++){
cout<<L[i]<<' ';
}
cout<<endl;
}
5113

被折叠的 条评论
为什么被折叠?



