#include<iostream>
using namespace std;
int a[50000];
void merge(int left, int mid,int right)
{
int temp[50000];
int t = 0;//记录temp的元素个数
int i = left;
int j = mid+1;
while (i <= mid && j <= right)
{
if (a[i] <= a[j]) temp[t++] = a[i++];
else temp[t++] = a[j++];
}
while(i <= mid) temp[t++] = a[i++];
while (j<=right) temp[t++] = a[j++];
for (int i = 0; i < t; i++)
{
a[left + i] = temp[i];
}
}
void mergeSort(int left, int right)
{
if (left < right)
{
int mid = (left + right) / 2;
mergeSort(left, mid);
mergeSort(mid + 1, right);
merge(left, mid, right);//合并
}
}
int main()
{
int n = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
mergeSort(0,n - 1);
for (int i = 0; i < n; i++)
{
cout<<a[i]<<" ";
}
cout << endl;
}
07-17
3万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-01
773
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)