// Merge Sort example
// For input array
// 2 8 7 1 3 5 6 4 9 0
//
// First partion in L[] and R[] will be
// L[] = 2 8 7 1 3 ==> L[] = 2 8 7 | R[] = 1 3 ==> L[] = 2 8 | R[] = 7
// R[] = 5 6 4 9 0
//
#include <stdio.h>
#define EOA 100000 //End of array
// To enable debug messages uncomment #define
// #define DEBUG 1
#ifdef DEBUG
# define D(x) x
#else
# define D(x)
#endif
void mergesort(int arr[], int p, int r);
void merge(int arr[], int p, int q, int r);
int arr[10] = {2, 8, 7, 1, 3, 5, 6, 4, 9, 0};
int main()
{
int i = 0;
for (i = 0; i <= 9; i++) {
}
mergesort(arr, 0, 9);
for (i = 0; i <= 9; i++) {
}
return 0;
}
void mergesort(int arr[], int p, int r)
{
if (p < r) {
int q = (p + r) / 2;
mergesort(arr, p, q);
mergesort(arr, q+1, r);
merge(arr, p, q, r);
}
}
void merge(int arr[], int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int i = 0;
int j = 0;
int L[15];
int R[15];
//Copy elements from p to n1 in L[]
for (i = 0; i < n1; i++) {
L[i] = arr[p + i];
}
L[i] = EOA;
for (j = 0; j < n2; j++) {
R[j] = arr[q + j + 1];
}
R[j] = EOA;
int lindx = 0;
int rindx = 0;
//Merge array L[] and R[]
for (i = p; i <= r; i++) {
if(L[lindx] <= R[rindx]) {
arr[i] = L[lindx++];
} else {
arr[i] = R[rindx++];
}
}
// Print debug statements
D
(printf("\n######################\n"));
for (i = 0; i < n1; i++) {
}
for (i = 0; i < n2; i++) {
}
for (i = p; i <= r; i++) {
}
}
Ly8gTWVyZ2UgU29ydCBleGFtcGxlCi8vIEZvciBpbnB1dCBhcnJheQovLyAyIDggNyAxIDMgNSA2IDQgOSAwCi8vCi8vIEZpcnN0IHBhcnRpb24gaW4gTFtdIGFuZCBSW10gd2lsbCBiZQovLyBMW10gPSAyIDggNyAxIDMgPT0+IExbXSA9IDIgOCA3IHwgUltdID0gMSAzID09PiBMW10gPSAyIDggfCBSW10gPS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pOwogICAgfQp9Cgp2b2lkIG1lcmdlKGludCBhcnJbXSwgaW50IHAsIGludCBxLCBpbnQgcikKewogICAgaW50IG4xID0gcSAtIHAgKyAxOwogICAgaW50IG4yID0gciAtIHE7CiAgICBpbnQgaSA9IDA7CiAgICBpbnQgaiA9IDA7CgogICAgaW50IExbMTVdOwogICAgaW50IFJbMTVdOwoKICAgIC8vQ29weSBlbGVtZW50cyBmcm9tIHAgdG8gbjEgaW4gTFtdCiAgICBmb3IgKGkgPSAwOyBpIDwgbjE7IGkrKykgewogICAgICAgIExbaV0gPSBhcnJbcCArIGldOwogICAgfQogICAgTFtpXSA9IEVPQTsKCiAgICBmb3IgKGogPSAwOyBqIDwgbjI7IGorKykgewogICAgICAgIFJbal0gPSBhcnJbcSArIGogKyAxXTsKICAgIH0KICAgIFJbal0gPSBFT0E7CgogICAgaW50IGxpbmR4ID0gMDsKICAgIGludCByaW5keCA9IDA7CiAgICAvL01lcmdlIGFycmF5IExbXSBhbmQgUltdCiAgICBmb3IgKGkgPSBwOyBpIDw9IHI7IGkrKykgewogICAgICAgIGlmKExbbGluZHhdIDw9IFJbcmluZHhdKSB7CiAgICAgICAgICAgIGFycltpXSA9IExbbGluZHgrK107CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgYXJyW2ldID0gUltyaW5keCsrXTsKICAgICAgICB9CiAgICB9CgogICAgLy8gUHJpbnQgZGVidWcgc3RhdGVtZW50cwogICAgRChwcmludGYoIlxuIyMjIyMjIyMjIyMjIyMjIyMjIyMjI1xuIikpOwogICAgRChwcmludGYoIkxlZnQgYXJyYXlcbiIpKTsKCiAgICBmb3IgKGkgPSAwOyBpIDwgbjE7IGkrKykgewogICAgICAgIEQocHJpbnRmKCIlZCAiLCBMW2ldKSk7CiAgICB9CgogICAgRChwcmludGYoIlxuUmlnaHQgYXJyYXlcbiIpKTsKICAgIGZvciAoaSA9IDA7IGkgPCBuMjsgaSsrKSB7CiAgICAgICAgRChwcmludGYoIiVkICIsIFJbaV0pKTsKICAgIH0KCiAgICBEKHByaW50ZigiXG5BZnRlciBNZXJnZVxuIikpOwogICAgZm9yIChpID0gcDsgaSA8PSByOyBpKyspIHsKICAgICAgICBEKHByaW50ZigiJWQgIiwgYXJyW2ldKSk7CiAgICB9CiAgICBEKHByaW50ZigiXG5cbiIpKTsKfQo=