-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
79 lines (64 loc) · 1.79 KB
/
Copy pathMergeSort.cpp
File metadata and controls
79 lines (64 loc) · 1.79 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define no cout << "NO\n"
#define yes cout << "YES\n"
#define endl cout << '\n'
#define vint vector<int>
#define vll vector<ll>
#define pb push_back
#define wl int t; cin >> t; while(t--)
#define sort(v) sort(v.begin(),v.end())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
// merge two subarrays
// first is arr[st..mid]
// second is arr[m+1..end]
void merge(vint &v, int left, int mid, int right){
// create temp arrays
vint L(mid - left + 1), R(right - mid);
//copy data to temp arrays L[] and R[]
for(int i = 0; i < L.size(); i++) L[i] = v[left+i];
for(int z = 0; z < R.size(); z++) R[z] = v[mid+1+z];
//merge the temp arrays back into arr[l..r]
int i = 0, z = 0, k = left;
while (i < L.size() && z < R.size()){
if(L[i] < R[z]){
v[k] = L[i++];
}else{
v[k] = R[z++];
}
k++;
}
//copy the remaining elements of L[], if there are any
while (i < L.size()){
v[k] = L[i++];
k++;
}
//copy the remaining elements of R[], if there are any
while (z < R.size()){
v[k] = R[z];
k++; z++;
}
}
void mergesort(vint &v, int l, int r){
if(l < r){
//same as (l+r)/2 but avoid overflow for large l and h
int mid = l + (r - l) / 2;
//sort the first and second halves
mergesort(v, l, mid);
mergesort(v, mid + 1, r);
merge(v, l, mid, r);
}
}
void printVector(vint v){
for(auto &i :v) cout << i << ' ';
}
int main() {
/* the complexity of merge sort is: O(n*log n) */
vint v = {14, 11, 1, 5, 20};
printf("Given array is:\n");
printVector(v);
mergesort(v, 0, v.size()-1);
printf("sorted array is:\n");
printVector(v);
}