-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.cpp
More file actions
executable file
·39 lines (35 loc) · 1.06 KB
/
selection_sort.cpp
File metadata and controls
executable file
·39 lines (35 loc) · 1.06 KB
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
#include <iostream>
#include <vector>
#include <ctime>
#include <iterator>
#include <algorithm>
#include "io.hpp"
template <class T>
void selection_sort(std::vector<T>& v)
{
for (auto i = v.begin(); i != v.end(); ++i) {
auto smallest = i;
for (auto j = i + 1; j != v.end(); ++j) {
if (*j < *smallest) {
smallest = j;
}
}
std::iter_swap(i, smallest);
}
}
int main(int argc, char* argv[])
{
std::string source_file("unsorted.bin");
if (argc > 1) {
source_file = argv[1];
std::cout << "Reading data from " << source_file << std::endl;
}
auto data = read_values<unsigned>(source_file.c_str());
clock_t t0 = clock();
selection_sort<unsigned>(data);
clock_t t1 = clock();
double spent = 1000 * static_cast<double>(t1 - t0) / CLOCKS_PER_SEC;
std::cout << "Array size: " << data.size() << std::endl;
std::cout << "Time spent: " << spent << " ms" << std::endl;
write_values<unsigned>("selection_sort.sorted", data);
}