-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.cpp
74 lines (71 loc) · 1.8 KB
/
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
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
#include <bits/stdc++.h>
using namespace std;
void merge(int start, int mid, int end, vector<int> &v)
{
int ArrayOneSize = mid - start + 1, ArrayTwoSize = end - mid;
vector<int> ArrayOne(ArrayOneSize), ArrayTwo(ArrayTwoSize);
for (int i = 0; i < ArrayOne.size(); i++)
{
ArrayOne[i] = v[i + start];
}
for (int i = 0; i < ArrayTwo.size(); i++)
{
ArrayTwo[i] = v[i + mid + 1];
}
int IndexArrayOne = 0, IndexArrayTwo = 0, IndexMergeArray = start;
while (IndexArrayOne < ArrayOneSize && IndexArrayTwo < ArrayTwoSize)
{
if (ArrayOne[IndexArrayOne] <= ArrayTwo[IndexArrayTwo])
{
v[IndexMergeArray] = ArrayOne[IndexArrayOne];
IndexArrayOne++;
}
else
{
v[IndexMergeArray] = ArrayTwo[IndexArrayTwo];
IndexArrayTwo++;
}
IndexMergeArray++;
}
while (IndexArrayOne < ArrayOneSize)
{
v[IndexMergeArray] = ArrayOne[IndexArrayOne];
IndexArrayOne++;
IndexMergeArray++;
}
while (IndexArrayTwo < ArrayTwoSize)
{
v[IndexMergeArray] = ArrayTwo[IndexArrayTwo];
IndexArrayTwo++;
IndexMergeArray++;
}
}
void MergeSort(int start, int end, vector<int> &v)
{
if (start >= end)
{
return;
}
int mid = (start + end) / 2;
MergeSort(start, mid, v);
MergeSort(mid + 1, end, v);
merge(start, mid, end, v);
}
int main(int argc, char const *argv[])
{
int n;
cout << "Enter the number of elements in Array\n";
cin >> n;
vector<int> arr(n);
cout << "Enter the " << n << " elements\n";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
MergeSort(0, arr.size() - 1, arr);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
return 0;
}