#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);
void mergesort(void *base, size_t n, size_t size, int (*compar)(const void *, const void *));
int compare_newdouble(const void *left, const void *right)
{
double *p = (double*)left;
double *q = (double*)right;
{
return 0;
}
{
return 1;
}
return -1;
}
void mergesort(void *base, size_t n, size_t size, int (*compar)(const void *, const void *))
{
const size_t mid = n/2;
size_t l = 0, r = 0, a = 0;
size_t i;
if(n < 2)
{
return;
}
mergesort(base, mid, size, compar); //left array
mergesort(base+size*mid, n-mid, size, compar); //right array
while(l < mid && r < (n-mid))
{
if((*compar)(base+size*l, base+size*mid+size*r) == -1)
{
memcpy((add
+size
*a
), (base
+size
*l
), size
); // putting the memory from left to add a++;
l++;
}
else
{
memcpy((add
+size
*a
), (base
+size
*mid
+size
*r
), size
); // putting the memory from right to add a++;
r++;
}
}
// Здесь были циклы
if(l < mid)
{
memcpy((add
+size
*a
), (base
+size
*l
), size
* (mid
- l
)); }
else if(r < (n-mid))
{
memcpy((add
+size
*a
), (base
+size
*mid
+size
*r
), size
* (n
-mid
- r
));
}
}
int main(void)
{
int n = 15, i;
double *a;
for(i = 0; i < n; i++)
{
a[i] = n - i;
}
mergesort(a, n, sizeof(double), &compare_newdouble);
for(i = 0; i < n; i++)
{
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8bWF0aC5oPgoKI2RlZmluZSBFUFMgMUUtNgoKaW50IGNvbXBhcmVfbmV3ZG91YmxlKGNvbnN0IHZvaWQgKmxlZnQsIGNvbnN0IHZvaWQgKnJpZ2h0KTsKdm9pZCBtZXJnZXNvcnQodm9pZCAqYmFzZSwgc2l6ZV90IG4sIHNpemVfdCBzaXplLCBpbnQgKCpjb21wYXIpKGNvbnN0IHZvaWQgKiwgY29uc3Qgdm9pZCAqKSk7CgoKCmludCBjb21wYXJlX25ld2RvdWJsZShjb25zdCB2b2lkICpsZWZ0LCBjb25zdCB2b2lkICpyaWdodCkKewogICAgZG91YmxlICpwID0gKGRvdWJsZSopbGVmdDsKICAgIGRvdWJsZSAqcSA9IChkb3VibGUqKXJpZ2h0OwogICAgCiAgICBpZihmYWJzKGZhYnMoKnApIC0gZmFicygqcSkpIDwgRVBTKQogICAgewogICAgICAgIHJldHVybiAwOwogICAgfQogICAgCiAgICBpZihmYWJzKCpwKSA+IGZhYnMoKnEpKQogICAgewogICAgICAgIHJldHVybiAxOwogICAgfQogICAgCiAgICByZXR1cm4gLTE7Cn0KCnZvaWQgbWVyZ2Vzb3J0KHZvaWQgKmJhc2UsIHNpemVfdCBuLCBzaXplX3Qgc2l6ZSwgaW50ICgqY29tcGFyKShjb25zdCB2b2lkICosIGNvbnN0IHZvaWQgKikpCnsKICAgIGNvbnN0IHNpemVfdCBtaWQgPSBuLzI7CiAgICBzaXplX3QgbCA9IDAsIHIgPSAwLCBhID0gMDsKICAgIHNpemVfdCBpOwogICAgdm9pZCAqYWRkPW1hbGxvYyhzaXplKm4pOwogICAgCiAgICBpZihuIDwgMikKICAgIHsKICAgICAgICByZXR1cm47CiAgICB9CiAgICAKICAgIG1lcmdlc29ydChiYXNlLCBtaWQsIHNpemUsIGNvbXBhcik7IC8vbGVmdCBhcnJheQogICAgbWVyZ2Vzb3J0KGJhc2Urc2l6ZSptaWQsIG4tbWlkLCBzaXplLCBjb21wYXIpOyAvL3JpZ2h0IGFycmF5CiAgICAKICAgIHdoaWxlKGwgPCBtaWQgJiYgciA8IChuLW1pZCkpCiAgICB7CiAgICAgICAgaWYoKCpjb21wYXIpKGJhc2Urc2l6ZSpsLCBiYXNlK3NpemUqbWlkK3NpemUqcikgPT0gLTEpCiAgICAgICAgewogICAgICAgICAgICBtZW1jcHkoKGFkZCtzaXplKmEpLCAoYmFzZStzaXplKmwpLCBzaXplKTsgLy8gcHV0dGluZyB0aGUgbWVtb3J5IGZyb20gbGVmdCB0byBhZGQKICAgICAgICAgICAgYSsrOwogICAgICAgICAgICBsKys7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIG1lbWNweSgoYWRkK3NpemUqYSksIChiYXNlK3NpemUqbWlkK3NpemUqciksIHNpemUpOyAvLyBwdXR0aW5nIHRoZSBtZW1vcnkgZnJvbSByaWdodCB0byBhZGQKICAgICAgICAgICAgYSsrOwogICAgICAgICAgICByKys7CiAgICAgICAgfQogICAgfQogICAgCiAgICAvLyDQl9C00LXRgdGMINCx0YvQu9C4INGG0LjQutC70YsKICAgIAogICAgaWYobCA8IG1pZCkKICAgIHsKICAgICAgICBtZW1jcHkoKGFkZCtzaXplKmEpLCAoYmFzZStzaXplKmwpLCBzaXplICogKG1pZCAtIGwpKTsKICAgIH0KICAgIGVsc2UgaWYociA8IChuLW1pZCkpCiAgICB7CiAgICAgICAgbWVtY3B5KChhZGQrc2l6ZSphKSwgKGJhc2Urc2l6ZSptaWQrc2l6ZSpyKSwgc2l6ZSAqIChuLW1pZCAtIHIpKTsKCiAgICB9CiAgICAKICAgIG1lbWNweShiYXNlLCBhZGQsIHNpemUqbik7CiAgICAKICAgIGZyZWUoYWRkKTsKfQoKaW50IG1haW4odm9pZCkKewogICAgaW50IG4gPSAxNSwgaTsKICAgIGRvdWJsZSAqYTsKICAgIAogICAgYT1tYWxsb2Moc2l6ZW9mKGRvdWJsZSkqbik7CiAgICAKICAgIGZvcihpID0gMDsgaSA8IG47IGkrKykKICAgIHsKICAgICAgICBhW2ldID0gbiAtIGk7CiAgICB9CiAgICAKICAgIG1lcmdlc29ydChhLCBuLCBzaXplb2YoZG91YmxlKSwgJmNvbXBhcmVfbmV3ZG91YmxlKTsKICAgIAogICAgZm9yKGkgPSAwOyBpIDwgbjsgaSsrKQogICAgewogICAgICAgIHByaW50ZigiJWxmICIsIGFbaV0pOwogICAgfQogICAgCiAgICBmcmVlKGEpOwogICAgCiAgICByZXR1cm4gMDsKfQo=