#include <iostream>
#include <typeinfo>
#define N 100
using namespace Std;
void mergesort (int *r,int low,int Mid,int high) {
Divide and conquer the law to R[low. High] for two-way merge sort
int *r1=new int[high-low+1];
int i,j,p=0;
i=low;j=mid+1;
if (! R1) {
cout<< "Space allocation failure";
return;}
while (I<=mid&&j<=high) {
R1[p++]= (R[i]<=r[j])? R[i++]:r[j++]; Two sub-files are not empty when they are output to r1[p]
}
while (I<=mid) {//If the 1th sub-file is not empty, copy the remaining records into R1
R1[p++]=r[i++];
}
while (J<=high) {//If the 2nd sub-file is not empty, copy the remaining records into R1
R1[p++]=r[j++];
}
The for (p=0,i=low;i<=high;i++,p++) {//merge results are copied back to R[low after completion: High
R[I]=R1[P];
}
delete [] R1;
}
void Merge (int *r,int low,int high) {
int mid;
Mid= (Low+high)/2;
if (LowMerge (R,low,mid);//recursive sort [low]. Mid
Merge (R,mid+1,high);//recursive sort [Mid+1..high]
MergeSort (R,low,mid,high);//merge left and right into an ordered sequence
}
}
int main ()
{
int N,low,high;
int r[n];
cin>>n;
for (int i=0;i<n;i++)
cin>>r[i];
low=0; high=n-1;
Merge (R,low,high);
for (int i=0;i<n;i++)
cout<<r[i]<< "";
return 0;
}
2-Way Merge sort