Data Structures and Algorithms - Arrays
Data Structures and Algorithms - Arrays
merge(arr, l, m, r);
}
}
void merge(int arr[], int l, int m, int r) /* Merge the temp arrays back into
{ arr[l..r]*/
int i, j, k; i = 0; // Initial index of first subarray
j = 0; // Initial index of second
int n1 = m - l + 1;
subarray
int n2 = r - m;
k = l; // Initial index of merged
subarray
/* create temp arrays */ while (i < n1 && j < n2) {
int L[n1], R[n2]; if (L[i] <= R[j]) {
arr[k] = L[i];
/* Copy data to temp arrays L[] and i++;
R[] */ }
for (i = 0; i < n1; i++) else {
L[i] = arr[l + i]; arr[k] = R[j];
for (j = 0; j < n2; j++) j++;
}
R[j] = arr[m + 1 + j];
k++;
}
/* Copy the remaining elements of L[], if
there
/* l is for left index and r
are any */ is right index of the
while (i < n1) {
arr[k] = L[i]; sub-array of arr to be
i++; sorted */
k++;
}