Skip to content

Instantly share code, notes, and snippets.

@abhijangda
Last active December 20, 2015 22:18
Show Gist options
  • Select an option

  • Save abhijangda/6203768 to your computer and use it in GitHub Desktop.

Select an option

Save abhijangda/6203768 to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
void merge(int **p,int x, int y, int z)
{
int *ar=*p;
int i=0,j=0,k=0;
while(i<=x && j<=z)
{
if(ar[i]<ar[j])
{
ar[k]=ar[i];
i++;
}
else
{
ar[k]=ar[j];
j++;
}
k++;
}
if(i==x && j!=z)
{
while(j<=z)
{
ar[k]=ar[j];
k++;
j++;
}
}
if(i!=x && j==z)
{
while(x<=x)
{
ar[k]=ar[i];
i++;
k++;
}
}
}
void merge_sort(int *p,int a, int b)
{
if(a<b)
{
int c=(a+b)/2;
merge_sort(p,a,c);
merge_sort(p,c+1,b);
merge(&p,a,c,b);
}
return;
}
int main()
{
int n;
cout<<"Enter the number of elements to be sorted: ";
cin>>n;
int A[n];
cout<<"Enter the elements:\n";
for(int k=0;k<n;k++)
cin>>A[k];
merge_sort(A ,0,n-1);
cout<<"Sorted array:\n";
for(int k=0;k<n;k++)
cout<<A[k]<<"\t";
cout<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment