#include<iostream>
using namespace std;
void merge(int a[], int low, int mid, int high)
{
int n1 = mid-low + 1;
int n2 = high - mid;
int* L = (int*)malloc(sizeof(int)*n1);
int* R = (int*)malloc(sizeof(int)*n2);
int i, j, k;
for (i = 0, k = low; i < n1; i++, k++)
L[i] = a[k];
for (j = 0, k = mid + 1; j < n2; j++, k++)
R[j] = a[k];//数组复制完成
for (i = 0, j = 0, k = low; i < n1 && j < n2; k++)
{
if (L[i] < R[j])
{
a[k] = L[i];
i++;
}
else
{
a[k] = R[j];
j++;
}
}
while (i < n1)
{
a[k] = L[i];
i++, k++;
}
while (j < n2)
{
a[k] = R[j];
j++, k++;
}
}
void mergesort(int a[], int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
mergesort(a, low, mid);
mergesort(a, mid + 1, high);
merge(a, low, mid, high);
}
}
int main()
{
int arr[8] = { 10,8,5,0,45,12,74,56 };
cout << "归并排序之后的:" << endl;
mergesort(arr, 0, 7);
for (int i = 0; i < 8; i++)
{
cout << arr[i]<<" ";
}
return 0;
}
标签:归并,int,二路,mid,high,++,low,n1,排序
From: https://blog.csdn.net/weixin_73598089/article/details/140462360