#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void merge(int L[], int left_count, int R[], int right_count, int a[]){
int i = 0, j = 0, k = 0;
while(i < left_count && j < right_count){
if(L[i] <= R[j])
a[k] = L[i++];
else
a[k] = R[j++];
k++;
}
if(i == left_count)
memcpy(&a
[k
], &R
[j
], sizeof(R
[0]) * (right_count
- j
)); else
memcpy(&a
[k
], &L
[j
], sizeof(L
[0]) * (left_count
- i
)); }
void mergesort(int a[], int n){
int i;
int split = n / 2;
int *L
= (int *)malloc(split
* sizeof(a
[0])); int *R
= (int *)malloc((n
- split
) * sizeof(a
[0]));
if(n > 1){
for(i = 0; i < split; i++)
L[i] = a[i];
for(i = split; i < n; i++)
R[i - split] = a[i];
mergesort(L, split);
mergesort(R, n - split);
merge(L, split, R, n - split, a);
}
}
int main(int argc, char *argv[]){
int i;
int v[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
mergesort(v, 10);
for(i = 0; i < 10; i++)
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdm9pZCBtZXJnZShpbnQgTFtdLCBpbnQgbGVmdF9jb3VudCwgaW50IFJbXSwgaW50IHJpZ2h0X2NvdW50LCBpbnQgYVtdKXsKCWludCBpID0gMCwgaiA9IDAsIGsgPSAwOwoKCXdoaWxlKGkgPCBsZWZ0X2NvdW50ICYmIGogPCByaWdodF9jb3VudCl7CgkJaWYoTFtpXSA8PSBSW2pdKQoJCQlhW2tdID0gTFtpKytdOwoJCWVsc2UKCQkJYVtrXSA9IFJbaisrXTsKCQlrKys7Cgl9CglpZihpID09IGxlZnRfY291bnQpCgkJbWVtY3B5KCZhW2tdLCAmUltqXSwgc2l6ZW9mKFJbMF0pICogKHJpZ2h0X2NvdW50IC0gaikpOwoJZWxzZQoJCW1lbWNweSgmYVtrXSwgJkxbal0sIHNpemVvZihMWzBdKSAqIChsZWZ0X2NvdW50IC0g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