I wrote this program to ask the user to give how many items to be sorted and then the program will display the original and sorted arrangement of values using merge sort in C programming language.
I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.
Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking and Arduino Project development at a very affordable price.
My personal website is http://www.jakerpomperada.com
My programming website is http://www.jakerpomperada.blogspot.com
I am also a book author you can purchase my books on computer programming and information technology in the following links below.
https://www.mindshaperspublishing.com/
https://www.unlimitedbooksph.com/
Sample Program Output
Program Listing
merge.cpp
#include<iostream>
using namespace std;
int Merge(int A[],int p, int q,int r)
{
int n1=q-p+1,i,j,k;
int n2=r-q;
int L[n1],R[n2];
for(i=0;i<n1;i++)
{
L[i]=A[p+i];
}
for(j=0;j<n2;j++)
{
R[j]=A[q+j+1];
}
i=0,j=0;
for(k=p;i<n1&&j<n2;k++)
{
if(L[i]<R[j])
{
A[k]=L[i++];
}
else
{
A[k]=R[j++];
}
}
while(i<n1)
{
A[k++]=L[i++];
}
while(j<n2)
{
A[k++]=R[j++];
}
}
int MergeSort(int A[],int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
MergeSort(A,p,q);
MergeSort(A,q+1,r);
Merge(A,p,q,r);
}
}
int main()
{
int A_Size=0;
cout <<"\n\n";
cout <<"\tMerge Sort in C++";
cout <<"\n\n";
cout<<"\tHow many elements? :";
cin>>A_Size;
int A[A_Size];
for(int i=0;i<A_Size;i++)
{
cout <<"\tEnter value on element no. " <<i+1 << " : ";
cin>>A[i];
}
cout<<"\n\n";
cout <<"\tOriginal Arrangement";
cout<<"\n\n";
cout <<"\t";
for(int i=0;i<A_Size;i++)
{
cout<<" " <<A[i]<<" ";
}
cout<<"\n\n";
cout <<"\tSorted Arrangement";
cout<<"\n\n";
cout <<"\t";
MergeSort(A,0,A_Size-1);
for(int i=0;i<A_Size;i++)
{
cout<<" "<<A[i]<<" ";
}
cout<<"\n\n";
cout <<"\tEnd of Program";
cout<<"\n\n";
}