This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.cpp
66 lines (60 loc) · 1.55 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
#include <bits/stdc++.h>
using namespace std;
void merge_sort(vector<int> *arr);
void print_vect(vector<int> arr);
void _merge_sort(vector<int> *arr, int start, int end);
void merge(vector<int> *arr, int start, int mid, int end);
int main()
{
vector<int> arr = {5, 4, 3, 2, 1, 6, 8, 6, 4, 3, 2, 1, 5, 7};
int size = arr.size();
print_vect(arr);
merge_sort(&arr);
print_vect(arr);
}
void print_vect(vector<int> arr)
{
cout << "[";
for (int x : arr)
cout << x << " ";
cout << "]" << endl;
}
void merge_sort(vector<int> *arr)
{
_merge_sort(arr, 0, (*arr).size() - 1);
}
void _merge_sort(vector<int> *arr, int start, int end)
{
if (start >= end)
return;
int mid = (start + end) / 2;
_merge_sort(arr, start, mid);
_merge_sort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
void merge(vector<int> *arr, int start, int mid, int end)
{
int size1 = mid - start + 1;
int size2 = end - mid;
int left[size1], right[size2];
for (int i = 0; i < size1; i++)
left[i] = (*arr)[i + start];
for (int i = 0; i < size2; i++)
right[i] = (*arr)[i + mid + 1];
int l = 0, r = 0, index = start;
while (l < size1 && r < size2)
{
if (left[l] < right[r])
{
(*arr)[index++] = left[l++];
}
else
{
(*arr)[index++] = right[r++];
}
}
while (l < size1)
(*arr)[index++] = left[l++];
while (r < size2)
(*arr)[index++] = right[r++];
}