标签:style class blog code java http
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorithm> #include<cassert> using namespace std; const int k=5; const int n=7; int a[n]={5, 5, 1, 2 , 5, 4, 1}; int b[n]; int c[k+1]; int main() { int maxn=0; memset(c, 0, sizeof c); for(int i=0;i<n;i++) { c[a[i]]++; maxn=max(maxn, a[i]); } assert(maxn<=k); //统计<=i的个数, c[i]就是最终i要放置的位置 for(int i=1;i<=maxn;i++) { c[i]+=c[i-1]; } for(int i=0;i<=maxn;i++) printf("%d ", c[i]); printf("\n"); for(int i=n-1;i>=0;i--) { b[c[a[i]] - 1]=a[i];//从0开始 --c[a[i]]; } for(int i=0;i<n;i++) printf("%d ", b[i]); printf("\n"); return 0; }
counting sort 计数排序,布布扣,bubuko.com
标签:style class blog code java http
原文地址:http://www.cnblogs.com/cute/p/3781382.html