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
3#ifndef TETL_ALGORITHM_GNOME_SORT_HPP
4#define TETL_ALGORITHM_GNOME_SORT_HPP
5
9
10namespace etl {
11
16template <typename BidirIt, typename Compare>
17constexpr auto gnome_sort(BidirIt first, BidirIt last, Compare comp) -> void
18{
19 auto i = first;
20 while (i != last) {
21 if (i == first or not comp(*i, *etl::prev(i))) {
22 ++i;
23 } else {
25 --i;
26 }
27 }
28}
29
34template <typename BidirIt>
35constexpr auto gnome_sort(BidirIt first, BidirIt last) -> void
36{
37 etl::gnome_sort(first, last, less());
38}
39
40} // namespace etl
41
42#endif // TETL_ALGORITHM_GNOME_SORT_HPP
constexpr auto iter_swap(ForwardIt1 a, ForwardIt2 b) -> void
Swaps the values of the elements the given iterators are pointing to.
Definition iter_swap.hpp:19
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:17
constexpr auto prev(BidirIt it, typename iterator_traits< BidirIt >::difference_type n=1) -> BidirIt
Return the nth predecessor of iterator it.
Definition prev.hpp:14
Definition adjacent_find.hpp:8
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:14