#include <stdlib.h>
#include <iostream>
using namespace std;
void MergeSort(double *Array,int left,int right)
{
if (left>=right)//只有一个元素,不用排序
{
return;
}
int middle=(left+right)/2;
MergeSort(Array,left,middle);
MergeSort(Array,middle+1,right);
//归并
int left_pointer=left;
int right_pointer=middle+1;
double *mergeArray=new double[right-left+1];
int new_array_pointer=0;
while (left_pointer<=middle&&right_pointer<=right)
{
if (Array[left_pointer]<=Array[right_pointer])
{
mergeArray[new_array_pointer++]=Array[left_pointer++];
}
else {
mergeArray[new_array_pointer++]=Array[right_pointer++];
}
}
//判断一下是那边的数组先读完了
if (left_pointer<=middle)//右边的先读完了
{
memcpy(&mergeArray[new_array_pointer],&Array[left_pointer],(middle-left_pointer+1)*sizeof(double));
}
else{//左边的读完了
memcpy(&mergeArray[new_array_pointer],&Array[right_pointer],(right-right_pointer+1)*sizeof(double));
}
memcpy(&Array[left],&mergeArray[0],(right-left+1)*sizeof(double));
delete mergeArray;
}
void main()
{
double A[]={2,4,3,8,6,13,5};
int n=7;
MergeSort(&A[0],0,6);
for (int i=0;i<n;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
delete A;
}