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