算法特点:
1.稳定排序
2.可用于链式结构,且不需要附加存贮空间,但递归时需要开辟相应的栈。
3.时间复杂度O(nlogn)空间复杂度O(n);
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void Merge(int *c,int *b,int low,int mid,int high){
int k=low,t=mid+1,r=low;
while(r<=mid&&t<=high){
if(c[r]<=c[t]) {
b[k++]=c[r++];
}else b[k++]=c[t++];
}
if(r>mid) while(t<=high) b[k++]=c[t++];
if(t>high) while(r<=mid) b[k++]=c[r++];
}
void MSort(int *a,int *b,int low,int high){
int c[100];
if(low==high) b[low]=a[low];
else{
int mid=(low+high)/2;
MSort(a,c,low,mid);
MSort(a,c,mid+1,high);
Merge(c,b,low,mid,high);
}
}
int main()
{
int a[9]={
49,38,65,97,76,13,27,49};
MSort(a,a,0,7);
for(int i=0;i<=7;i++)
cout<<a[i]<<" ";
//cout << "Hello world!" << endl;
return 0;
}