#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define EPS 1E-6
int compare_newdouble(const void *left, const void *right);
int compare_newdouble(const void *left, const void *right)
{
double *p = (double*)left;
double *q = (double*)right;
{
return 0;
}
{
return 1;
}
return -1;
}
void arrswap(void *base, void *add);
void arrswap(void *base, void *add)
{
void *c;
c=base;
base=add;
add=c;
}
void merge(void *base, void *add, size_t mid, size_t n, size_t size, int (*compar)(const void *, const void *));
void merge(void *base, void *add, size_t mid, size_t n, size_t size, int (*compar)(const void *, const void *))
{
size_t l = 0, r = 0, a = 0;
while(l < mid && r < (n-mid))
{
if((*compar)((char*)base+size*l, (char*)base+size*mid+size*r) == -1)
{
memcpy(((char*)add
+size
*a
), ((char*)base
+size
*l
), size
); // putting the memory from left to add a++;
l++;
}
else
{
memcpy(((char*)add
+size
*a
), ((char*)base
+size
*mid
+size
*r
), size
); // putting the memory from right to add a++;
r++;
}
}
if(l < mid)
{
memcpy(((char*)add
+size
*a
), ((char*)base
+size
*l
), size
*(mid
- l
)); }
if(r < (n-mid))
{
memcpy(((char*)add
+size
*a
), ((char*)base
+size
*mid
+size
*r
), size
*(n
-mid
- r
)); }
}
void mergesort(void *base, size_t n, size_t size, int (*compar)(const void *, const void *));
void mergesort(void *base, size_t n, size_t size, int (*compar)(const void *, const void *))
{
size_t mid = n/2;
if(n < 2) // array length = 1
{
return;
}
mergesort(base, mid, size, compar); //left array
mergesort((char*)base+size*mid, n-mid, size, compar); //right array
merge(base, add, n/2, n, size, compar);
arrswap(base, add);
}
int main(void)
{
FILE *inp;
FILE *outp;
int n, i;
double *arr;
inp
=fopen("input.txt", "r"); outp
=fopen("output.txt", "w");
if(inp==NULL)
{
perror("Error. File input.txt cannot be found"); return -1;
}
for(i = 0; i < n; i++)
{
}
mergesort(arr, n, sizeof(double), &compare_newdouble);
for(i = 0; i < n; i++)
{
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI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gewogICAgICAgIGlmKCgqY29tcGFyKSgoY2hhciopYmFzZStzaXplKmwsIChjaGFyKiliYXNlK3NpemUqbWlkK3NpemUqcikgPT0gLTEpCiAgICAgICAgewogICAgICAgICAgICBtZW1jcHkoKChjaGFyKilhZGQrc2l6ZSphKSwgKChjaGFyKiliYXNlK3NpemUqbCksIHNpemUpOyAvLyBwdXR0aW5nIHRoZSBtZW1vcnkgZnJvbSBsZWZ0IHRvIGFkZAogICAgICAgICAgICBhKys7CiAgICAgICAgICAgIGwrKzsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgbWVtY3B5KCgoY2hhciopYWRkK3NpemUqYSksICgoY2hhciopYmFzZStzaXplKm1pZCtzaXplKnIpLCBzaXplKTsgLy8gcHV0dGluZyB0aGUgbWVtb3J5IGZyb20gcmlnaHQgdG8gYWRkCiAgICAgICAgICAgIGErKzsKICAgICAgICAgICAgcisrOwogICAgICAgIH0KICAgIH0KCiAgICBpZihsIDwgbWlkKQogICAgewogICAgICAgIG1lbWNweSgoKGNoYXIqKWFkZCtzaXplKmEpLCAoKGNoYXIqKWJhc2Urc2l6ZSpsKSwgc2l6ZSoobWlkIC0gbCkpOwogICAgfQoKICAgIGlmKHIgPCAobi1taWQpKQogICAgewogICAgICAgIG1lbWNweSgoKGNoYXIqKWFkZCtzaXplKmEpLCAoKGNoYXIqKWJhc2Urc2l6ZSptaWQrc2l6ZSpyKSwgc2l6ZSoobi1taWQgLSByKSk7CiAgICB9Cn0KCnZvaWQgbWVyZ2Vzb3J0KHZvaWQgKmJhc2UsIHNpemVfdCBuLCBzaXplX3Qgc2l6ZSwgaW50ICgqY29tcGFyKShjb25zdCB2b2lkICosIGNvbnN0IHZvaWQgKikpOwp2b2lkIG1lcmdlc29ydCh2b2lkICpiYXNlLCBzaXplX3Qgbiwgc2l6ZV90IHNpemUsIGludCAoKmNvbXBhcikoY29uc3Qgdm9pZCAqLCBjb25zdCB2b2lkICopKQp7CiAgICBz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