-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.cpp
83 lines (72 loc) · 1.76 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
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <chrono>
#include <string>
using namespace std;
void print(const vector<string>& v) {
for (auto i : v)
cout << i << " ";
cout << "\n";
}
template<typename IteratorType>
void merge(IteratorType first, IteratorType last) {
IteratorType mid = first + distance(first, last) / 2;
vector<string> temp;
IteratorType it1 = first;
IteratorType it2 = mid + 1;
while (it1 <= mid && it2 <= last) {
if (*it1 < *it2) {
temp.push_back(*it1);
it1++;
}
else {
temp.push_back(*it2);
it2++;
}
}
if (it1 > mid) {
while (it2 <= last) {
temp.push_back(*it2);
it2++;
}
}
else {
while(it1 <= mid) {
temp.push_back(*it1);
it1++;
}
}
int i = 0;
for (IteratorType jt = first; jt != last; jt++) {
*(jt) = temp.at(i);
i++;
}
}
void msort(auto first, auto last) {
auto mid = first + distance(first, last) / 2;
if(first < mid) msort(first, mid);
if(mid < last) msort(mid + 1, last);
merge(first, last);
}
int main() {
vector<string> v;
fstream in;
in.open("plik.txt", ios::in);
string input;
while (in >> input) {
v.push_back(input);
}
cout << "Before merge sort: \n";
print(v);
auto t1 = chrono::high_resolution_clock::now();
msort(v.begin(), v.end());
auto t2 = chrono::high_resolution_clock::now();
auto dt = chrono::duration_cast<chrono::microseconds>(t2 - t1);
cout << "After merge sort: \n";
print(v);
cout << "Merge sort duration: " << dt.count() << " milisekund.\n";
in.close();
return 0;
}