-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.cpp
40 lines (40 loc) · 942 Bytes
/
MergeSort.cpp
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
38
39
40
#include<bits/stdc++.h>
using namespace std;
void merge(vector<int>& arr, int l, int mid, int r) {
vector<int> copy(arr.begin()+l,arr.begin()+mid+1);
int n=copy.size();
int i=0, j=mid+1;
while(i<n&&j<=r) {
if(copy[i]<=arr[j])
arr[l++]=copy[i++];
else if(copy[i]>arr[j])
arr[l++]=arr[j++];
}
while(i<n)
arr[l++]=copy[i++];
while(j<=r)
arr[l++]=arr[j++];
}
void mergeSort(vector<int>& arr, int l, int r) {
if(l<r) {
// int mid=(l+r)/2;
int mid=l+(r-l)/2;
// int mid=r-(r-l)/2;
mergeSort(arr,l,mid);
mergeSort(arr,mid+1,r);
merge(arr,l,mid,r);
}
}
int main(void) {
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<endl<<"Sorted Array"<<endl;
mergeSort(arr,0,n-1);
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}