void Merge(int left, int mid, int right, int *array_A, int *array_B) {
int i = left;
int j = mid + 1;
int k = left;
while(i <= mid && j <= right)
array_B[k++] = (array_A[i] > array_A[j]) ? array_A[j++] : array_A[i++];
while (i <= mid)
array_B[k++] = array_A[i++];
while (j <= right)
array_B[k++] = array_A[j++];
}
void Mpass(int *array_A, int *array_B ,int len, int n) {
int i = 0;
while (i < (n - (len * 2) + 1)) {
Merge( i, i + len -1, i + len * 2 - 1, array_A, array_B);
i += len*2;
}
if (i + len < n)
Merge(i, i + len - 1, n-1, array_A, array_B);
else {
for (; i<n ;i++)
array_B[i] = array_A[i];
}
}
void merge_sort(int *array, int n) {
int len = 1;
int *array_A = array;
int array_B[n];
while(len < n) {
Mpass(array_A, array_B, len, n);
len *= 2;
Mpass(array_B, array_A, len, n);
len *= 2;
}
}
|