#include <iostream>
using namespace std;
void mergesort(int [],int,int);
int* merge(int list[], int , int);
void main(){
int size,*list=NULL,i;
cout << "Input number of elements:\n";
cin >> size;
list = new int [size];
cout <<"Input elements of the list:\n";
for(i=0;i<size;i++)
> *(list+i);
mergesort(list,0,size-1);
cout <<"The sorted list:\n";
for(i=0;i<size;i++)
}
int* merge(int list[],int low,int high){ //merge two sorted list
int *buffer =new int[(high-low)+1],mid = (low+high)/2,l1=low,l2 = mid+1,k=0;
/*
**********************************
precondition :this function can only merge two sorted list ,i.e 1 ,3 5;2 ,4 6
*********************************
look at l1 & l2 ,if l1 > l2 ,copy bufer[l1] to result[k] ,k(buffer index)++,l1++
else if l2 > l1 copy bufer[l2] to result[k] ,k,l2++
if l1 ends before l2 ,copy remained l2 to result[k]
AND rest
*/
while(l1 <= mid && l2 <= high){
if(list[l1] <= list[l2])
buffer[k++] = list[l1++];
else
buffer[k++] = list[l2++];
}
// copy the rest ,l1 ends first ,copy remained l2 to buffer
if(l1 > mid)
while(l2 <= high)
buffer[k++] = list[l2++];
else
while(l1 <= mid)
buffer[k++]=list[l1++];
return buffer;
}
void mergesort (int a[], int low, int high){
// this algorithm sorts the portion of array a with index
// from low to high, inclusive.
if (low <high) {
// recursively sort a[low..mid]
mergesort(a,low,mid);
// recursively sort a[mid+1..high]
mergesort(a,mid+1,high);
// merge the two sorted lists into buffer[]
buffer = merge(a,low,high);
// copy buffer[] into a[low..high]
for(int i=low,j=0;i<=high;i++,j++)
a[i] = buffer[j];
delete [] buffer;
}
}
using namespace std;
void mergesort(int [],int,int);
int* merge(int list[], int , int);
void main(){
int size,*list=NULL,i;
cout << "Input number of elements:\n";
cin >> size;
list = new int [size];
cout <<"Input elements of the list:\n";
for(i=0;i<size;i++)
> *(list+i);
mergesort(list,0,size-1);
cout <<"The sorted list:\n";
for(i=0;i<size;i++)
}
int* merge(int list[],int low,int high){ //merge two sorted list
int *buffer =new int[(high-low)+1],mid = (low+high)/2,l1=low,l2 = mid+1,k=0;
/*
**********************************
precondition :this function can only merge two sorted list ,i.e 1 ,3 5;2 ,4 6
*********************************
look at l1 & l2 ,if l1 > l2 ,copy bufer[l1] to result[k] ,k(buffer index)++,l1++
else if l2 > l1 copy bufer[l2] to result[k] ,k,l2++
if l1 ends before l2 ,copy remained l2 to result[k]
AND rest
*/
while(l1 <= mid && l2 <= high){
if(list[l1] <= list[l2])
buffer[k++] = list[l1++];
else
buffer[k++] = list[l2++];
}
// copy the rest ,l1 ends first ,copy remained l2 to buffer
if(l1 > mid)
while(l2 <= high)
buffer[k++] = list[l2++];
else
while(l1 <= mid)
buffer[k++]=list[l1++];
return buffer;
}
void mergesort (int a[], int low, int high){
// this algorithm sorts the portion of array a with index
// from low to high, inclusive.
if (low <high) {
// recursively sort a[low..mid]
mergesort(a,low,mid);
// recursively sort a[mid+1..high]
mergesort(a,mid+1,high);
// merge the two sorted lists into buffer[]
buffer = merge(a,low,high);
// copy buffer[] into a[low..high]
for(int i=low,j=0;i<=high;i++,j++)
a[i] = buffer[j];
delete [] buffer;
}
}










