计数排序的步骤
1、统计数组中每个值为i的元 素出现的次数,存入数组C 的第i项
2、根据C[i],整理排序结果
//简单计数排序
#define MaxNum 100
void CountSort(int R[], int n)
{
int i, j, k;
int C[MaxNum] = { 0 };
for (i = 0; i < n; ++i)
{
C[R[i]]++;
}
k = 0;
for (j = 0; j <= MaxNum; j++)
{
for (i = 1; i <= C[j]; i++)
R[k++] = j;
}
}
void main()
{
int a[] = { 6,1,12,6,18,7,0,6 };
int n = sizeof(a) / sizeof(int);
CountSort(a, n);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/89458cda2ac7a5c1ec752e1acc22ef0a.png)