fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int n; //initial no. of integers in array
  5. int *list; //pointer to the array of integers
  6. int *MergeSort(int *A, int x, int y); //definition, return type is pointer to integer array
  7.  
  8.  
  9. void main()
  10. {
  11. printf("Enter the no. of integers in the array: ");
  12. scanf("%d",&n);
  13.  
  14. list=(int *)malloc(n*sizeof(int));
  15.  
  16. for(int i=0;i<n;i++) //taking input
  17. {
  18. printf("\nEnter a no.: ");
  19. scanf("%d",&list[i]);
  20. }
  21.  
  22. MergeSort(list,0,n-1); //calling MergeSort on list from index 0 to index n-1
  23.  
  24. for(int i=0;i<n;i++) //printing output
  25. {
  26. printf("%d ",list[i]);
  27. }
  28.  
  29. }
  30.  
  31. int *MergeSort(int *A, int x, int y) //declaration
  32. {
  33. if(x==y) //base case, return the 1-element array itself
  34. {
  35. return A;
  36. }
  37.  
  38. else
  39. {
  40. int size=1+y-x; //total no. of elements
  41. int half=(x+y)/2; //halving the array
  42.  
  43. MergeSort(A, x, half); //calling MergeSort on 1st half of array
  44. MergeSort(A, half+1, y); //calling MergeSort on 2nd half of array
  45.  
  46.  
  47. int *C; //pointer to 3rd array
  48. C=(int *)malloc(size*sizeof(int));
  49. int j=x; //indicator for first half
  50. int k=half; //indicator for 2nd half
  51. int i=0; //indicator for 3rd array
  52.  
  53.  
  54. while((j<=half)||(k<=y)) //till all the elements from either half are not exhausted
  55. {
  56. if(A[j]<=A[k]) //if element of first half is smaller, put that in C
  57. {
  58. C[i]=A[j];
  59. j++;
  60. }
  61. else //otherwise put element of second half in C
  62. {
  63. C[i]=A[k];
  64. k++;
  65. }
  66. i++; //increment indicator for C
  67. }
  68.  
  69.  
  70. if(j==(half+1)) //if first half is finished
  71. {
  72. while(i<size) //transfer all elements of second half in C
  73. {
  74. C[i]=A[k];
  75. i++;
  76. k++;
  77. }
  78. }
  79. else if(k==(y+1)) //if second half is finished
  80. {
  81. while(i<size) //transfer all elements of 1st half in C
  82. {
  83. C[i]=A[j];
  84. i++;
  85. j++;
  86. }
  87. }
  88.  
  89.  
  90.  
  91. return C; //return C
  92. }
  93.  
  94. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:9:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main()
      ^
prog.c: In function ‘main’:
prog.c:16:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
  for(int i=0;i<n;i++) //taking input
  ^
prog.c:16:2: note: use option -std=c99 or -std=gnu99 to compile your code
prog.c:24:10: error: redefinition of ‘i’
  for(int i=0;i<n;i++) //printing output
          ^
prog.c:16:10: note: previous definition of ‘i’ was here
  for(int i=0;i<n;i++) //taking input
          ^
prog.c:24:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
  for(int i=0;i<n;i++) //printing output
  ^
stdout
Standard output is empty