归并排序,先把代码存下来,以后再写文章
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn], b[maxn];
void Merge(int a[], int b[], int First, int mid, int End)
{
int fl = First;
int fr = mid+1;
int t = First;
while(fl <= mid && fr <= End){
if(a[fl] < a[fr]){
b[t++] = a[fl++];
}
else{
b[t++] = a[fr++];
}
}
while(fl <= mid){
b[t++] = a[fl++];
}
while(fr <= End){
b[t++] = a[fr++];
}
for(int i=First; i<=End; i++){
a[i] = b[i];
}
}
void MergeSort(int a[], int b[], int First, int End)
{
if(First < End){
int mid = (First + End)/2;
MergeSort(a, b, First, mid);
MergeSort(a, b, mid+1, End);
Merge(a, b, First, mid, End);
}
}
int main()
{
int n;
cin >> n;
for(int i=0; i<n; i++)
cin >> a[i];
MergeSort(a, b, 0, n-1);
for(int i=0; i<n; i++){
cout << a[i] << ' ';
}
cout << endl;
return 0;
}