算法导论 计数排序

#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;

}
原文地址:https://www.cnblogs.com/riverphoenix/p/2408818.html