fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define SIZE 30
  7.  
  8. void countsort(int a[], int n);
  9.  
  10. int main()
  11. {
  12. int array[SIZE],i;
  13.  
  14. srand(time(0));
  15.  
  16. printf("Your array: ");
  17. for(i=0;i<SIZE;i++){
  18. array[i]=rand()%9;
  19. printf("%d ", array[i]);
  20. }
  21.  
  22. countsort(array, sizeof(array)/sizeof(array[0]));
  23.  
  24. printf("\nSorted array:");
  25. for(i=0;i<SIZE;i++){
  26. printf("%d ",array[i]);
  27. }
  28.  
  29. return 0;
  30. }
  31.  
  32. void countsort(int a[], int n)
  33. {
  34.  
  35. int i,j,k, count [9] = { 0 };
  36.  
  37. for(i=0; i < n; ++i)
  38. {
  39. ++count[a[i]];
  40. }
  41.  
  42. j = 0;
  43. for(i = 0; i< 9; ++i)
  44. {
  45. if (count[i])
  46. for(k = 0; k < count[i]; ++k)
  47. a[j++] = i;
  48. }
  49. }
  50.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Your array: 0 2 1 2 8 8 0 3 4 8 1 0 1 2 5 7 1 1 7 3 1 7 1 7 1 5 4 6 8 8 
Sorted array:0 0 0 1 1 1 1 1 1 1 1 2 2 2 3 3 4 4 5 5 6 7 7 7 7 8 8 8 8 8