-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathmerge_sort.cpp
67 lines (63 loc) · 1.56 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
#include <bits/stdc++.h>
using namespace std;
// Function that performs the sorting operation
void merge(vector<int>& v, int l, int mid, int r){
int i = l, j = mid+1;
vector<int> b;
// Compares the elements of the two arrays and sorts them
while (i<=mid && j<=r)
{
if (v[i]>v[j]){
b.push_back(v[j]);
j++;
}
else{
b.push_back(v[i]);
i++;
}
}
// If the 1st array is exhausted, then copies the remaining elements of the 2nd array to the 1st array
while (j<=r){
b.push_back(v[j]);
j++;
}
// If the 2nd array is exhausted, then copies the remaining elements of the 1st array to the 2nd array
while (i<=mid){
b.push_back(v[i]);
i++;
}
// Copies all the elements of the array 'b' to array 'arr'
for (int i = l; i <= r; i++){
v[i] = b[i-l];
}
return ;
}
// Function that divides the array into half at each step and sorts them
void mergeSort(vector<int>& v, int l, int r){
if (l<r)
{
int mid = l + (r-l)/2;
// Sorts the left array
mergeSort(v, l, mid);
// Sorts the right array
mergeSort(v, mid+1, r);
// Merges the left and the right array
merge(v, l, mid, r);
}
}
int main()
{
// Total number of elements in vector
int n;
cin >> n;
vector<int> v(n);
for(int i=0; i<n; i++){
cin >> v[i];
}
mergeSort(v, 0, n-1);
cout << "After sorting:\n";
for(auto i: v){
cout << i << " ";
}
return 0;
}