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
8
9namespace etl {
10
18template <typename RandomIt, typename Compare>
19constexpr 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
34template <typename RandomIt>
35constexpr 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
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
Definition adjacent_find.hpp:8
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:14