-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.hpp
More file actions
44 lines (35 loc) · 1.17 KB
/
insertion_sort.hpp
File metadata and controls
44 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
/// Problem sources:
/// https://en.wikipedia.org/wiki/Insertion_sort
#ifndef FORFUN_SORTING_INSERTION_SORT_HPP
#define FORFUN_SORTING_INSERTION_SORT_HPP
#include <algorithm>
#include <iterator>
namespace forfun::sorting {
/// @note The strategy assumes that @p first and @p last point to a non-empty
/// range of elements, otherwise the behavior of the strategy is undefined.
template <typename Iter, typename Sentinel>
requires std::sortable<Iter>
and std::bidirectional_iterator<Iter>
and std::sentinel_for<Iter, Sentinel>
auto insertion_sort(Iter const first, Sentinel const last) noexcept -> void
{
using std::iter_swap;
using std::less;
using std::next;
using std::prev;
for (Iter it_i{next(first)}; it_i != last; ++it_i)
{
for (
Iter it_j{it_i}; it_j != first && less{}(*it_j, *prev(it_j)); --it_j
)
{
iter_swap(it_j, prev(it_j));
}
}
}
} // namespace forfun::sorting
#endif // FORFUN_SORTING_INSERTION_SORT_HPP