#include<iostream>
using namespace std;
const int N = 1000010;
int n, a[N];
void merge_sort(int a[], int l, int mid, int r)
{
int* t = new int[r - l + 1];
int i = l, j = mid + 1;
int id = 0;
while (i <= mid && j <= r)
{
if (a[i] >= a[j]) t[id++] = a[i++];
else t[id++] = a[j++];
}
while (i <= mid) t[id++] = a[i++];
while (j <= r) t[id++] = a[j++];
for (int i = 0; i < id; i++)
{
a[l + i] = t[i];
}
delete[]t;
}
void merge_up2down(int a[], int l, int r)
{
if (l >= r)
return;
int mid = l + r >> 1;
merge_up2down(a, l, mid);
merge_up2down(a, mid + 1, r);
merge_sort(a, l, mid, r);
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
merge_up2down(a, 1, n);
for (int i = 1; i <= n; i++)
cout << a[i] << endl;
return 0;
}
归并排序。
最新推荐文章于 2024-08-31 15:48:32 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)