codex
Loading...
Searching...
No Matches
selection.hpp
Go to the documentation of this file.
1#ifndef CODEX_SORT_SELECTION_H
2#define CODEX_SORT_SELECTION_H
3
4#include <algorithm>
5#include <cassert>
6
7namespace codex {
8
9template<std::input_iterator I, std::sentinel_for<I> S>
10void check_range(I b, S e) {
11 if constexpr(!std::random_access_iterator<I>)
12 return;
13 else if constexpr(!std::random_access_iterator<S>)
14 return;
15 else
16 assert(b <= e);
17}
18
19template<std::forward_iterator I, std::sentinel_for<I> S>
20I min_element(I b, S e) {
21 check_range(b, e);
22 if(b == e)
23 return e;
24 auto ret = b;
25 for(auto i = std::next(ret); i != e; ++i) {
26 assert(std::min_element(b, i) == ret);
27 if(*i < *ret)
28 ret = i;
29 }
30 assert(std::min_element(b, e) == ret);
31 return ret;
32}
33
34template<std::permutable I, std::sentinel_for<I> S>
35void selection_sort(I b, S e) {
36 check_range(b, e);
37 for(auto i = b; i != e; ++i) {
38 assert(std::is_sorted(b, i));
39 std::iter_swap(i, min_element(i, e));
40 assert(std::is_partitioned(
41 b, e, [i](const auto &x) { return x < *i; }));
42 }
43 assert(std::is_sorted(b, e));
44}
45
46}
47
48#endif
static const struct board b
Definition chess0.c:57
#define x
Definition gcc14.c:1
Definition rotate.hpp:6
constexpr I min_element(I b, S e)
Definition selection.hpp:20
void check_range(I b, S e)
Definition selection.hpp:10
constexpr void selection_sort(I b, S e)
Definition selection.hpp:39
Definition reflexpr.cpp:168
Definition mult_inh.c:26