/* Bismillahir Rahmanir Rahim
Implementation of Merge Sort using C++
*/
#include<bits/stdc++.h>
using namespace std;
int ar[1000];
int marg_partition(int st, int mid , int ed){
int arr[1000], left, right, j=0, i;
left=st;
right=mid+1;
while(left<=mid && right<=ed){
if(ar[left]<ar[right]){
arr[j++]=ar[left];
left++;
}
else{
arr[j++]=ar[right];
right++;
}
}
if(left>mid){
for(i=right;i<=ed;i++)
arr[j++]=ar[i];
}
else {
for(i=left;i<=mid;i++)
arr[j++]=ar[i];
}
for(i=0;i<j;i++) ar[i+st]=arr[i];
return 0;
}
int marg_sort(int st,int ed){
if(st<ed){
int mid=(st+ed)/2;
marg_sort(st,mid);
marg_sort(mid+1,ed);
marg_partition(st,mid,ed);
}
}
int main(){
int i,j,n,k;
cin>>n;
for(i=0;i<n;i++) cin>>ar[i];
marg_sort(0,n-1);
for(i=0;i<n;i++) cout<<ar[i]<<" ";
cout<<endl;
return 0;
}
|
Monday, April 24, 2017
Merge Sort - Implementation with C++
Subscribe to:
Post Comments (Atom)
-
#include<bits/stdc++.h> #define ll long long using namespace std ; ll n , k , t_case ; ll bigmod ( ll b , ll p , ll m...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3...
No comments:
Post a Comment