fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void counting_sort(int A[], int length)
  4. {
  5. int i; int frequency[10]; int *SortedArray;
  6. for(i=0;i<10;i++)
  7. {frequency[i]=0;}
  8. SortedArray=(int *)calloc(length,sizeof(int));
  9. for(i=0;i<length;i++)
  10. {SortedArray[i]=-3;}
  11. for(i=0;i<length;i++)
  12. {frequency[A[i]]+=1;}
  13.  
  14. for(i=1;i<10;i++)
  15. {frequency[i]+=frequency[i-1];}
  16. // for(i=0;i<10;i++)
  17. // {printf("%d\n",frequency[i]);}
  18.  
  19. int count=0;
  20. while(count<length)
  21. {
  22. for(i=0;i<10;i++)
  23. {
  24. if(frequency[i]!=0 && SortedArray[frequency[i]-1]==-3)
  25. {
  26. SortedArray[frequency[i]-1]=i;
  27. frequency[i]-=1;
  28. count+=1;
  29. }
  30. }
  31. }
  32.  
  33. for(i=0;i<length;i++)
  34. {
  35. A[i]=SortedArray[i];
  36. }
  37.  
  38. }
  39. int main(void) {
  40. // data is in between 0 to 9
  41. int array[200];
  42. int response[200];
  43. int length; int i;
  44. scanf("%d",&length);
  45. for(i=0;i<length;i++)
  46. {scanf("%d",&array[i]);}
  47. counting_sort(array,length);
  48. // *response=counting_sort(array,length);
  49. for(i=0;i<length;i++)
  50. {printf("%d ",array[i]);}
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 9424KB
stdin
8
2 4 2 3 9 7 7 1
stdout
1  2  2  3  4  7  7  9