tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
gnome_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_GNOME_SORT_HPP
5#define TETL_ALGORITHM_GNOME_SORT_HPP
6
7#include <etl/_algorithm/iter_swap.hpp>
8#include <etl/_functional/less.hpp>
9#include <etl/_iterator/prev.hpp>
10
11namespace etl {
12
13/// \brief Sorts the elements in the range `[first, last)` in non-descending order.
14/// \details https://en.wikipedia.org/wiki/Gnome_sort
15/// \note Non-standard extension
16/// \ingroup algorithm
17template <typename BidirIt, typename Compare>
18constexpr auto gnome_sort(BidirIt first, BidirIt last, Compare comp) -> void
19{
20 auto i = first;
21 while (i != last) {
22 if (i == first or not comp(*i, *etl::prev(i))) {
23 ++i;
24 } else {
25 etl::iter_swap(i, etl::prev(i));
26 --i;
27 }
28 }
29}
30
31/// \brief Sorts the elements in the range `[first, last)` in non-descending order.
32/// \details https://en.wikipedia.org/wiki/Gnome_sort
33/// \note Non-standard extension
34/// \ingroup algorithm
35template <typename BidirIt>
36constexpr auto gnome_sort(BidirIt first, BidirIt last) -> void
37{
38 etl::gnome_sort(first, last, less());
39}
40
41} // namespace etl
42
43#endif // TETL_ALGORITHM_GNOME_SORT_HPP
constexpr auto gnome_sort(BidirIt first, BidirIt last) -> void
Sorts the elements in the range [first, last) in non-descending order.
Definition gnome_sort.hpp:36
constexpr auto gnome_sort(BidirIt first, BidirIt last, Compare comp) -> void
Sorts the elements in the range [first, last) in non-descending order.
Definition gnome_sort.hpp:18
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15