-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.cpp
74 lines (63 loc) · 1.6 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
#include <iostream>
#include <vector>
#include <utility>
/*
worst: O(n log n)
*/
template<typename Comparable>
void Merge(int leftPos, int rightPos, int rightEnd, std::vector<Comparable>* const outData, std::vector<Comparable>* const outTmp)
{
int leftEnd = rightPos - 1;
int tmpPos = leftPos;
int numElements = rightEnd - leftPos + 1;
while(leftPos <= leftEnd && rightPos <= rightEnd)
{
if(outData->at(leftPos) <= outData->at(rightPos))
{
outTmp->at(tmpPos++) = std::move(outData->at(leftPos++));
}
else
{
outTmp->at(tmpPos++) = std::move(outData->at(rightPos++));
}
}
while(leftPos <= leftEnd)
{
outTmp->at(tmpPos++) = std::move(outData->at(leftPos++));
}
while(rightPos <= rightEnd)
{
outTmp->at(tmpPos++) = std::move(outData->at(rightPos++));
}
for(int i = 0; i < numElements; ++i, --rightEnd)
{
outData->at(rightEnd) = std::move(outTmp->at(rightEnd));
}
}
template<typename Comparable>
void MergeSort(const int left, const int right, std::vector<Comparable>* const outData, std::vector<Comparable>* const outTmp)
{
if(left < right)
{
int center = (left + right) / 2;
MergeSort(left, center, outData, outTmp);
MergeSort(center + 1, right, outData, outTmp);
Merge(left, center + 1, right, outData, outTmp);
}
}
template<typename Comparable>
void MergeSort(std::vector<Comparable>* const outData)
{
std::vector<Comparable> tmp(int(outData->size()));
MergeSort(0, int(tmp.size() - 1), outData, &tmp);
}
int main()
{
std::vector<int> a1{ 8, 6, 4, 4, 1, 9, 2, 78, 25, 45, 11, 35 };
MergeSort(&a1);
for(const auto& x : a1)
{
std::cout << x << ' ';
}
std::cout << '\n';
}