int *B = (int *)malloc((n+1) * sizeof(int));
void Merge(int a[], int low, int mid, int high)
{
for(int k = low; k <= high; k++)
{
B[k] = a[k];
}
for(i = low, j = mid + 1, k = i; i < mid && j <= high; k++)
{
if(B[i] <= B[j])
{
a[k] = B[i];
i++;
}
else
{
a[k] = B[j++];
}
}
while(i <= mid) a[k++] = B[i++];
while(j <= high) a[k++] = B[j++];
}
void MergeSort(int a[], int low, int high)
{
if(low < high)
{
int mid = (low + high) /2;
MergeSort(a,low, mid);
MergeSort(a, mid, high);
Merge(a,low, mid, high);
}
}
void Merge(int a[], int low, int mid, int high)
{
for(int k = low; k <= high; k++)
{
B[k] = a[k];
}
for(i = low, j = mid + 1, k = i; i < mid && j <= high; k++)
{
if(B[i] <= B[j])
{
a[k] = B[i];
i++;
}
else
{
a[k] = B[j++];
}
}
while(i <= mid) a[k++] = B[i++];
while(j <= high) a[k++] = B[j++];
}
void MergeSort(int a[], int low, int high)
{
if(low < high)
{
int mid = (low + high) /2;
MergeSort(a,low, mid);
MergeSort(a, mid, high);
Merge(a,low, mid, high);
}
}