-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-sort.cpp
77 lines (65 loc) · 1.75 KB
/
merge-sort.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
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
#include <vector>
#include <iostream>
using namespace std;
void mergeArray(vector<int> &arr, int low, int mid, int high) {
int i=low, j=mid+1, k=low;
vector<int> temp(arr.size());
// merged left and right sub-arrays in sorted manner
while(i<=mid && j<=high) {
// if left has smaller element, add it to temp
if(arr[i] < arr[j]) {
temp[k] = arr[i];
i++;
}
// else if right has smaller element, add it to temp
else {
temp[k] = arr[j];
j++;
}
k++;
}
// if left sub-array still has some elements
// this is the case when left sub-array has more elements than right sub-array
while(i <= mid) {
temp[k] = arr[i];
i++;
k++;
}
// if right sub-array still has some elements
// this is the case when right sub-array has more elements than left sub-array
while(j <= high) {
temp[k] = arr[j];
j++;
k++;
}
// finally update changes in the original array
for(int s=low; s<=high; s++) {
arr[s] = temp[s];
}
}
void mergeSort(vector<int> &arr, int low, int high) {
int mid;
if(low < high) {
// find mid
mid = (low+high)/2;
// keep dividing array till it has only one element
// since a single elemnet is always sorted
mergeSort(arr, low, mid);
mergeSort(arr, mid+1, high);
// start merging array such that the merged array is sorted
mergeArray(arr, low, mid, high);
}
}
int main() {
int n;
cin>>n;
vector<int> v(n);
for(int i=0; i<n; i++) {
cin>>v[i];
}
mergeSort(v, 0, n-1);
cout<<"Output : \n";
for(int i=0; i<n; i++) {
cout<<v[i]<<" ";
}
}