编辑代码

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

void CountSort(int *a, int len)
{
	assert(a);
	int max = a[0], min = a[0];
	for (int i = 0; i < len; i++){
		if (a[i] > max)
			max = a[i];
		if (a[i] < min)
			min = a[i];
	}
	int range = max - min + 1;
	int *b = (int *)calloc(range, sizeof(int));
	for (int i = 0; i < len; i++){
		b[a[i] - min] += 1;
	} 
	int j = 0;
	for (int i = 0; i < range; i++){
		while (b[i]--){
			a[j++] = i + min;
		}
	}
	free(b);
	b = NULL;
}
void PrintArray(int *a, int len)
{
	for (int i = 0; i < len; i++){
		printf("%d ", a[i]);
	}
	printf("\n");
}
int main()
{
	int a[] = { 2,5,3,0,2,3,0,3 };
	printf("排序前:");
	PrintArray(a, sizeof(a) / sizeof(int));
	CountSort(a, sizeof(a) / sizeof(int));
	printf("排序后:");
	PrintArray(a, sizeof(a) / sizeof(int));
	system("pause");
	return 0;
}