/*
归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。
将已有序的子序列合并,得到完全有序的序列;
即先使每个子序列有序,再使子序列段间有序。
若将两个有序表合并成一个有序表,称为二路归并。
归并排序是一种稳定的排序方法。
*/
#include<iostream>
#include<stdlib.h>
using namespace std ;
typedef int datetype ;
void Merger_sort(datetype *A ,datetype Start ,datetype End)
{
if(Start==End-1)
return ;
int Center ;
Center =(Start+End) /2 ;
Mergersort(A,Start,Center);
Mergersort(A,Center,End) ;
datetype *Temp = new datetype[End-Start] ;
int Left=Start ,Right=Center ,i=0;
while(Left<Center&&Right<End)
{
if(A[Left]<A[Right])
Temp[i++]=A[Left++];
else Temp[i++]=A[Right++];
}
while(Left<Center)
{
Temp[i++]=A[Left++];
}
while(Right<End)
{
Temp[i++]=A[Right++];
}
for(int idx =Start,i=0;i<End-Start;i++,idx++)
A[idx]=Temp[i];
delete [] Temp ;
}int main()
{
int a[10],i;
int n;
cout<<"请输入数组的长度!"<<endl ;
cin>>n;
cout<<"请输入数组元素"<<endl ;
for(i=0;i<n;i++)
cin>>a[i];
Merger_sort(a,0,n);
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl ;
}