-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.cpp
More file actions
executable file
·42 lines (37 loc) · 1.03 KB
/
Copy pathselection_sort.cpp
File metadata and controls
executable file
·42 lines (37 loc) · 1.03 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
40
41
42
// selection sort function
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
std::vector<int>::iterator find_smallest(
const std::vector<int>::iterator& beg,
const std::vector<int>::iterator& end) {
int smallest = *beg;
std::vector<int>::iterator ret = beg;
for (auto it = beg; it != end; ++it) {
if (*it < smallest) {
smallest = *it;
ret = it;
}
}
return ret;
}
void selection_sort(std::vector<int>& input) {
const auto end = input.end();
for (auto it = input.begin(); it != end; ++it) {
auto smallestIt = find_smallest(it, end);
std::swap(*it, *smallestIt);
}
}
int main() {
std::vector<int> input(100);
std::iota(input.begin(), input.end(), 0);
std::default_random_engine e(1);
std::shuffle(input.begin(), input.end(), e);
selection_sort(input);
for (auto i : input) {
std::cout << i << ' ';
}
std::cout << std::endl;
return 0;
}