forked from Morwenn/cpp-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sorter.cpp
More file actions
154 lines (139 loc) · 5.03 KB
/
Copy pathbubble_sorter.cpp
File metadata and controls
154 lines (139 loc) · 5.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2017 Morwenn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <cstddef>
#include <functional>
#include <iterator>
#include <type_traits>
#include <cpp-sort/sorter_facade.h>
#include <cpp-sort/sorter_traits.h>
#include <cpp-sort/utility/as_function.h>
#include <cpp-sort/utility/iter_move.h>
#include <cpp-sort/utility/size.h>
#include <cpp-sort/utility/static_const.h>
namespace detail
{
template<
typename ForwardIterator,
typename StrictWeakOrdering
>
auto bubble_sort(ForwardIterator first, std::size_t size,
StrictWeakOrdering compare)
-> void
{
if (size < 2) return;
auto&& comp = cppsort::utility::as_function(compare);
while (--size) {
ForwardIterator current = first;
ForwardIterator next = std::next(current);
for (std::size_t i = 0 ; i < size ; ++i) {
if (comp(*next, *current)) {
using cppsort::utility::iter_swap;
iter_swap(current, next);
}
++next;
++current;
}
}
}
struct bubble_sorter_impl
{
// Pair of iterators overload
template<
typename ForwardIterator,
typename StrictWeakOrdering = std::less<>,
typename = std::enable_if_t<not cppsort::is_projection_iterator_v<
StrictWeakOrdering, ForwardIterator
>>
>
auto operator()(ForwardIterator first, ForwardIterator last,
StrictWeakOrdering compare={}) const
-> void
{
static_assert(
std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<ForwardIterator>::iterator_category
>::value,
"bubble_sorter requires at least forward iterators"
);
bubble_sort(first, std::distance(first, last),
compare);
}
// Iterable overload
template<
typename ForwardIterable,
typename StrictWeakOrdering = std::less<>,
typename = std::enable_if_t<not cppsort::is_projection_v<
StrictWeakOrdering, ForwardIterable
>>
>
auto operator()(ForwardIterable&& iterable, StrictWeakOrdering compare={}) const
-> void
{
static_assert(
std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<decltype(std::begin(iterable))>::iterator_category
>::value,
"bubble_sorter requires at least forward iterators"
);
bubble_sort(std::begin(iterable), cppsort::utility::size(iterable),
compare);
}
// Sorter traits
using iterator_category = std::forward_iterator_tag;
using is_always_stable = std::true_type;
};
}
struct bubble_sorter:
cppsort::sorter_facade<detail::bubble_sorter_impl>
{};
namespace
{
constexpr auto&& bubble_sort
= cppsort::utility::static_const<bubble_sorter>::value;
}
#include <algorithm>
#include <array>
#include <cassert>
#include <numeric>
int main()
{
// Check that the bubble_sorter can sort any permutation
// of an array of 8 values
// Fill the collection in sorted order
std::array<int, 8> collection;
std::iota(std::begin(collection), std::end(collection), 0);
// Projection to sort in descending order
auto projection = [](int n) { return -n; };
// For each possible permutation of collection
do
{
auto to_sort = collection;
// Bubble sort the collection
bubble_sort(to_sort, projection);
// Check that it is sorted in descending order
assert(std::is_sorted(std::begin(to_sort), std::end(to_sort), std::greater<>{}));
} while (std::next_permutation(std::begin(collection), std::end(collection)));
}