fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void countingSort(int *array, int size);
  5. void printArray(int *array, int size);
  6. int findMax(int *array, int size);
  7.  
  8. int main(void) {
  9. int set[] = {41, 2, 17, 37, 22, 104, 86, 92, 11, 58, 34};
  10. int size = sizeof(set)/sizeof(set[]);
  11.  
  12. printf("Array before sorting: \n");
  13. printArray(set, size);
  14.  
  15. countingSort(set, size);
  16.  
  17. printf("\nArray after sorting: \n");
  18. printArray(set, size);
  19.  
  20. return 0;
  21. }
  22.  
  23. void printArray(int *array, int size){
  24. for(int i = 0; i < size; i++){
  25. printf("%d ", array[i]);
  26. }
  27. printf("\n");
  28. }
  29.  
  30. void countingSort(int *array, int size){
  31. int range = findMax(array, size) + 1;
  32. int count[range];
  33. int output[size];
  34. int i;
  35. for(i = 0; i < range; i++){
  36. count[i] = 0;
  37. }
  38.  
  39. for(i = 0; i < size; i++){
  40. ++count[array[i]];
  41. }
  42.  
  43. for(i = 1; i < range; i++){
  44. count[i] += count[i - 1];
  45. }
  46.  
  47. for(i = size - 1; i >= 0; i--){
  48. output[count[array[i]] - 1] = array[i];
  49. --count[array[i]];
  50. }
  51.  
  52. for(i = 0; i < size; i++){
  53. array[i] = output[i];
  54. }
  55. }
  56.  
  57. void printArray(int *array, int size){
  58. for(int i = 0; i < size; i++){
  59. printf("%d ", array[i]);
  60. }
  61. printf("\n");
  62. }
  63.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'main':
prog.c:10:36: error: expected expression before ']' token
  int size = sizeof(set)/sizeof(set[]);
                                    ^
prog.c: At top level:
prog.c:57:6: error: redefinition of 'printArray'
 void printArray(int *array, int size){
      ^
prog.c:23:6: note: previous definition of 'printArray' was here
 void printArray(int *array, int size){
      ^
stdout
Standard output is empty