tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
bubble_sort.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_BUBBLE_SORT_HPP
5#define TETL_ALGORITHM_BUBBLE_SORT_HPP
6
7#include <etl/_algorithm/iter_swap.hpp>
8#include <etl/_functional/less.hpp>
9
10namespace etl {
11
12/// \ingroup algorithm
13/// @{
14
15/// \brief Sorts the elements in the range `[first, last)` in non-descending
16/// order. The order of equal elements is guaranteed to be preserved.
17///
18/// https://en.wikipedia.org/wiki/Bubble_sort
19///
20/// \note Non-standard extension
21template <typename RandomIt, typename Compare>
22constexpr auto bubble_sort(RandomIt first, RandomIt last, Compare comp) -> void
23{
24 for (auto i = first; i != last; ++i) {
25 for (auto j = first; j < i; ++j) {
26 if (comp(*i, *j)) {
27 etl::iter_swap(i, j);
28 }
29 }
30 }
31}
32
33/// \brief Sorts the elements in the range `[first, last)` in non-descending
34/// order. The order of equal elements is guaranteed to be preserved.
35///
36/// https://en.wikipedia.org/wiki/Bubble_sort
37///
38/// \note Non-standard extension
39template <typename RandomIt>
40constexpr auto bubble_sort(RandomIt first, RandomIt last) -> void
41{
42 etl::bubble_sort(first, last, etl::less());
43}
44
45/// @}
46
47} // namespace etl
48
49#endif // TETL_ALGORITHM_BUBBLE_SORT_HPP
constexpr auto bubble_sort(RandomIt first, RandomIt last, Compare comp) -> void
Sorts the elements in the range [first, last) in non-descending order. The order of equal elements is...
Definition bubble_sort.hpp:22
constexpr auto bubble_sort(RandomIt first, RandomIt last) -> void
Sorts the elements in the range [first, last) in non-descending order. The order of equal elements is...
Definition bubble_sort.hpp:40
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15