//两路归并排序
template<class T>
void MergeSort(T Array[],T tempArray[],int left,int right){//Array为待排序的数组,left,right为两段下标
int middle;
if (left<right)
{
middle=(left+right)/2;
MergeSort(Array,tempArray,left,middle);//对左边的一半进行递归
MergeSort(Array,tempArray,middle+1,right);//对右边的一半进行递归
merge(Array,tempArray,left,right,middle);//进行归并
}
}
template<class T>
void merge(T Array[],T tempArraay[],int left,int right,int middle){//进行归并
for (int j=left;j<right;j++)
tempArraay[j]=Array[j];
int index1=left;
int index2=middle+1;
int i=left;
while(index1<=middle && index2<=right){
if(tempArraay[index1]<=tempArraay[index2])
Array[i++]=tempArraay[index1++];
else
Array[i++]=tempArraay[index2++];
}
while(index1<=middle)
Array[i++]=tempArraay[index1++];
while(index2<=right)
Array[i++]=tempArraay[index2++];
}
template<class T>
void MergeSort(T Array[],T tempArray[],int left,int right){//Array为待排序的数组,left,right为两段下标
int middle;
if (left<right)
{
middle=(left+right)/2;
MergeSort(Array,tempArray,left,middle);//对左边的一半进行递归
MergeSort(Array,tempArray,middle+1,right);//对右边的一半进行递归
merge(Array,tempArray,left,right,middle);//进行归并
}
}
template<class T>
void merge(T Array[],T tempArraay[],int left,int right,int middle){//进行归并
for (int j=left;j<right;j++)
tempArraay[j]=Array[j];
int index1=left;
int index2=middle+1;
int i=left;
while(index1<=middle && index2<=right){
if(tempArraay[index1]<=tempArraay[index2])
Array[i++]=tempArraay[index1++];
else
Array[i++]=tempArraay[index2++];
}
while(index1<=middle)
Array[i++]=tempArraay[index1++];
while(index2<=right)
Array[i++]=tempArraay[index2++];
}