#include<stdio.h>
void printArr(int a[], int length){
int i;
for(i=0;i<length;i++)
printf("%d, ",a[i]);
printf("\n");
}
void counting_sort(int a[],int length, int b[]){
int c[6],i;
for(i=0;i<6;i++)
c[i] = 0;
for(i=0;i<length;i++){
c[a[i]]++;
}
for(i=1;i<6;i++)
c[i] = c[i]+c[i-1];
printArr(c,6);
for(i=length-1;i>=0;i--){
b[c[a[i]]-1] = a[i];
c[a[i]]--;
}
}
int main(){
int a[] = {2,5,3,0,2,3,0,3};
int b[8];
printf("**************\n");
printArr(a,8);
counting_sort(a,8,b);
printf("*********\n");
printArr(b,8);
return 1;
}
计数排序
最新推荐文章于 2020-06-20 22:57:14 发布