-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectionsort.cpp
49 lines (43 loc) · 1.16 KB
/
selectionsort.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
#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 ssort(IteratorType first, IteratorType last) {
IteratorType kt;
for (IteratorType it = first; it != last - 1; it++) {
kt = it;
for (IteratorType jt = it + 1; jt != last; jt++) {
if(*jt < *kt) iter_swap(kt, jt);
iter_swap(it, kt);
}
}
}
int main() {
vector<string> v;
fstream in;
in.open("plik.txt", ios::in);
string input;
while (in >> input) {
v.push_back(input);
}
cout << "Before selection sort: \n";
print(v);
auto t1 = chrono::high_resolution_clock::now();
ssort<vector<string>::iterator>(v.begin(), v.end());
auto t2 = chrono::high_resolution_clock::now();
auto dt = chrono::duration_cast<chrono::microseconds>(t2 - t1);
cout << "After selection sort: \n";
print(v);
cout << "Selection sort duration: " << dt.count() << " milisekund.\n";
in.close();
return 0;
}