一种计数排序算法的实现
#include <stdio.h>int array[10] = {10, 25, 13, 13, 6, 12, 28, 15, 10, 22};
void counting_sort(int result[], int position[], int len, int max);
int main(int argc, char **argv)
{
int i = 0;
int result[10] = {0};
int position[30] = {0};
counting_sort(result, position, 10, 30);
for(i = 0; i < 10; i++)
{
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
void counting_sort(int result[], int position[], int len, int max)
{
int i = 0;
for(i = 0; i < len; i++)
{
position[array[i]] += 1;
}
for(i = 1; i < max; i++){
position[i] += position[i-1];
}
for(i = 0; i < len; i++)
{
result[position[array[i]]-1] = array[i];
position[array[i]]--;
}
}