riverphoenix

导航

 
#include<iostream>
using namespace std;
void counting_sort(int* &a, int length, int k, int* &b, int* &c)
{
for(int i = 0; i < k + 1; i++)
c[i] = 0;
for(int i = 0; i < length; i++)
c[a[i]]++;
for(int i = 1; i < k + 1; i++)
c[i] = c[i] + c[i-1];
for(int i = length - 1; i >= 0; i--)
{
b[c[a[i]] - 1] = a[i];
c[a[i]]--;
}
for(int i=0;i<length;i++)
{
cout<<b[i]<<" ";
cout<<endl;
}
}
int main()
{
const int LEN = 6;
int *a = new int[LEN];
for(int i = 0; i < LEN; i++)
a[i] = (i - 3)*(i - 3) + 4;
int *b = new int[LEN];
const int k=13;
int *c = new int[k];
counting_sort(a,LEN,k,b,c);

return 0;

}
posted on 2012-03-20 21:30  riverphoenix  阅读(177)  评论(0编辑  收藏  举报