**
计数排序
** 注:经测试代码有效
#include <iostream>
#include <stdlib.h>
using namespace std;
int sort_num(int *nums, int len, int m_nums)
{
int arr[12];
int *count = (int *)malloc(sizeof(int)*(m_nums));
memset(count, 0, sizeof(count)*m_nums);
for (int i = 0; i < len; i++)
{
count[nums[i]]++;
}
for (int k = 1; k < 10; k++)
{
count[k] = count[k] + count[k - 1];
}
for (int k = len-1; k>=0; k--)
{
arr[--count[nums[k]]] = nums[k];
}
for (int i = 0; i < len; i++)
printf("%d", arr[i]);
return 0;
}
int main()
{
int array[12] = { 2, 4, 9, 3, 2, 4, 8, 5, 7, 0, 6, 3};
sort_num(array, 12, 10);
}
|