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