Last active
December 20, 2015 22:18
-
-
Save abhijangda/6203768 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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