void merge(int A[], int p, int q, int r) {
int *tmpL, *tmpR;
int boundary;
int n1, n2;
int i, j, k;
n1 = q - p + 1;
n2 = r - q;
tmpL = (int *)malloc(sizeof(int) * (n1 + 1));
tmpR = (int *)malloc(sizeof(int) * (n2 + 1));
for (i = 0; i < n1; i++)
tmpL[i] = A[p + i];
for (j = 0; j < n2; j++)
tmpR[j] = A[q + j + 1];
boundary = tmpL[n1 - 1] > tmpR[n2 - 1] ? tmpL[n1 - 1] + 1 : tmpR[n2 - 1] + 1;
tmpL[n1] = boundary;
tmpR[n2] = boundary;
i = 0;
j = 0;
for (k = p; k <= r; k++) {
if (tmpL[i] <= tmpR[j]) {
A[k] = tmpL[i];
i++;
} else {
A[k] = tmpR[j];
j++;
}
}
free(tmpL);
free(tmpR);
}
void merge_sort(int A[], int p, int r) {
int q;
if (p < r) {
q = (p + r) / 2;
merge_sort(A, p, q);
merge_sort(A, q + 1, r);
merge(A, p, q, r);
}
}
I could not understand this infinite boundary code exactly boundary = tmpL[n1 - 1] > tmpR[n2 - 1] ? tmpL[n1 - 1] + 1 : tmpR[n2 - 1] + 1;
Thanks https://i.stack.imgur.com/UmyUg.png (circled in blue)
This is a conditional statement, A> B? C:D
.
If A> B
is true then evaluate C, else evaluate D.
But I still do not understand the boundary part.
Is this the same as adding two while loops to deal with when one half of them have remaining elements and append them to the end of new arrays?
If I don't initialize them as infinite boundary they may give me a segmentation fault.