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. list=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. int halfsize=(size/2);
  44. int *A1=(int *)malloc(halfsize*sizeof(int));
  45. int *A2=(int *)malloc((size-halfsize)*sizeof(int));
  46. A1=MergeSort(A, x, half); //calling MergeSort on 1st half of array
  47. A2=MergeSort(A, half+1, y); //calling MergeSort on 2nd half of array
  48.  
  49.  
  50. int *C; //pointer to 3rd array
  51. C=(int *)malloc(size*sizeof(int));
  52. int j=0; //indicator for first half
  53. int k=0; //indicator for 2nd half
  54. int i=0; //indicator for 3rd array
  55.  
  56.  
  57. while((j<=half)&&(k<=half)) //till all the elements from either half are not exhausted
  58. {
  59. if(A1[j]<=A2[k]) //if element of first half is smaller, put that in C
  60. {
  61. C[i]=A1[j];
  62. j++;
  63. }
  64. else //otherwise put element of second half in C
  65. {
  66. C[i]=A2[k];
  67. k++;
  68. }
  69. i++; //increment indicator for C
  70. }
  71. int flag;
  72. if(j==(half+1))
  73. {
  74. flag=1;
  75. }
  76. else if(k==(half+1))
  77. {
  78. flag=2;
  79. }
  80. if(flag==1) //if first half is finished
  81. {
  82. while(i<size) //transfer all elements of second half in C
  83. {
  84. C[i]=A2[k];
  85. i++;
  86. k++;
  87. }
  88.  
  89. }
  90. else if(flag==2) //if second half is finished
  91. {
  92. while(i<size) //transfer all elements of 1st half in C
  93. {
  94. C[i]=A1[j];
  95. i++;
  96. j++;
  97. }
  98. }
  99.  
  100.  
  101. for(int i=x;i<=y;i++) //copyback
  102. {
  103. A[x]=C[i-x];
  104. }
  105. free(A1);
  106. free(A2);
  107. free(C);
  108. return A;
  109. }
  110.  
  111.  
  112. }
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
  ^
prog.c: In function ‘MergeSort’:
prog.c:101:11: error: redefinition of ‘i’
   for(int i=x;i<=y;i++) //copyback
           ^
prog.c:54:7: note: previous definition of ‘i’ was here
   int i=0;   //indicator for 3rd array
       ^
prog.c:101:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
   for(int i=x;i<=y;i++) //copyback
   ^
stdout
Standard output is empty