tetl
0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
insertion_sort.hpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: BSL-1.0
2
3
#ifndef TETL_ALGORITHM_INSERTION_SORT_HPP
4
#define TETL_ALGORITHM_INSERTION_SORT_HPP
5
6
#include <
etl/_algorithm/iter_swap.hpp
>
7
#include <
etl/_functional/less.hpp
>
8
9
namespace
etl
{
10
18
template
<
typename
RandomIt,
typename
Compare>
19
constexpr
auto
insertion_sort
(RandomIt first, RandomIt last, Compare comp) ->
void
20
{
21
for
(
auto
i = first; i != last; ++i) {
22
auto
key = *i;
23
auto
j = i;
24
while
(j != first and comp(key, *(j - 1))) {
25
*j = *(j - 1);
26
--j;
27
}
28
*j = key;
29
}
30
}
31
34
template
<
typename
RandomIt>
35
constexpr
auto
insertion_sort
(RandomIt first, RandomIt last) ->
void
36
{
37
etl::insertion_sort
(first, last,
etl::less
());
38
}
39
40
}
// namespace etl
41
42
#endif
// TETL_ALGORITHM_INSERTION_SORT_HPP
etl::insertion_sort
constexpr auto insertion_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
insertion_sort.hpp:19
iter_swap.hpp
less.hpp
etl
Definition
adjacent_find.hpp:8
etl::less
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition
less.hpp:14
include
etl
_algorithm
insertion_sort.hpp
Generated on Sat Mar 8 2025 16:19:59 for tetl by
1.14.0