fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. #define EPS 1E-6
  7.  
  8. int compare_newdouble(const void *left, const void *right);
  9. int compare_newdouble(const void *left, const void *right)
  10. {
  11. double *p = (double*)left;
  12. double *q = (double*)right;
  13.  
  14. if(fabs(fabs(*p) - fabs(*q)) < EPS)
  15. {
  16. return 0;
  17. }
  18.  
  19. if(fabs(*p) > fabs(*q))
  20. {
  21. return 1;
  22. }
  23.  
  24. return -1;
  25. }
  26.  
  27. void arrswap(void *base, void *add);
  28. void arrswap(void *base, void *add)
  29. {
  30. void *c;
  31. c=base;
  32. base=add;
  33. add=c;
  34. }
  35.  
  36. void merge(void *base, void *add, size_t mid, size_t n, size_t size, int (*compar)(const void *, const void *));
  37. void merge(void *base, void *add, size_t mid, size_t n, size_t size, int (*compar)(const void *, const void *))
  38. {
  39. size_t l = 0, r = 0, a = 0;
  40.  
  41. while(l < mid && r < (n-mid))
  42. {
  43. if((*compar)((char*)base+size*l, (char*)base+size*mid+size*r) == -1)
  44. {
  45. memcpy(((char*)add+size*a), ((char*)base+size*l), size); // putting the memory from left to add
  46. a++;
  47. l++;
  48. }
  49. else
  50. {
  51. memcpy(((char*)add+size*a), ((char*)base+size*mid+size*r), size); // putting the memory from right to add
  52. a++;
  53. r++;
  54. }
  55. }
  56.  
  57. if(l < mid)
  58. {
  59. memcpy(((char*)add+size*a), ((char*)base+size*l), size*(mid - l));
  60. }
  61.  
  62. if(r < (n-mid))
  63. {
  64. memcpy(((char*)add+size*a), ((char*)base+size*mid+size*r), size*(n-mid - r));
  65. }
  66. }
  67.  
  68. void mergesort(void *base, size_t n, size_t size, int (*compar)(const void *, const void *));
  69. void mergesort(void *base, size_t n, size_t size, int (*compar)(const void *, const void *))
  70. {
  71. size_t mid = n/2;
  72. void* add=malloc(size*n);
  73.  
  74. if(n < 2) // array length = 1
  75. {
  76. return;
  77. }
  78.  
  79. mergesort(base, mid, size, compar); //left array
  80. mergesort((char*)base+size*mid, n-mid, size, compar); //right array
  81.  
  82. merge(base, add, n/2, n, size, compar);
  83.  
  84. arrswap(base, add);
  85.  
  86. memcpy(base, add, size*n);
  87.  
  88. free(add);
  89. }
  90.  
  91. int main(void)
  92. {
  93. FILE *inp;
  94. FILE *outp;
  95. int n, i;
  96. double *arr;
  97.  
  98. inp=fopen("input.txt", "r");
  99. outp=fopen("output.txt", "w");
  100.  
  101. if(inp==NULL)
  102. {
  103. perror("Error. File input.txt cannot be found");
  104. return -1;
  105. }
  106.  
  107. fscanf(inp, "%d ", &n);
  108. arr=malloc(sizeof(double)*n);
  109.  
  110. for(i = 0; i < n; i++)
  111. {
  112. fscanf(inp, "%lf ", &arr[i]);
  113. }
  114.  
  115. mergesort(arr, n, sizeof(double), &compare_newdouble);
  116.  
  117. for(i = 0; i < n; i++)
  118. {
  119. fprintf(outp, "%lf ", arr[i]);
  120. }
  121.  
  122. printf("Done. \n");
  123.  
  124. fclose(inp);
  125. fclose(outp);
  126. free(arr);
  127.  
  128. return 0;
  129. }
Runtime error #stdin #stdout #stderr 0s 2284KB
stdin
10

-10 9 8 -7 6 5.000001 5 -3 5 -1
stdout
Standard output is empty
stderr
Error. File input.txt cannot be found: Permission denied