TRIQS/itertools 1.3.0
C++ range library
Loading...
Searching...
No Matches
sort.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell, chuffa
16
21
22#ifndef _ITERTOOLS_SORT_HPP
23#define _ITERTOOLS_SORT_HPP
24
25#include <algorithm>
26#include <cstddef>
27#include <functional>
28#include <iterator>
29#include <ranges>
30#include <utility>
31
32namespace itertools {
33
38
57 template <std::forward_iterator ForwardIt, typename Compare = std::less<>>
58 std::size_t bubble_sort(ForwardIt first, ForwardIt last, Compare comp = {}) {
59 std::size_t n_swaps = 0;
60
61 for (auto unsorted_end = last; first != unsorted_end;) {
62 auto last_swap = first;
63 for (auto curr = first, next = std::next(first); next != unsorted_end; ++curr, ++next) {
64 if (comp(*next, *curr)) {
65 std::iter_swap(next, curr);
66 last_swap = next;
67 ++n_swaps;
68 }
69 }
70 unsorted_end = last_swap; // Everything after last_swap is now in final position
71 }
72
73 return n_swaps;
74 }
75
94 template <std::bidirectional_iterator BidirIt, typename Compare = std::less<>>
95 std::size_t insertion_sort(BidirIt first, BidirIt last, Compare comp = {}) {
96 if (first == last) return 0;
97
98 std::size_t n_swaps = 0;
99
100 for (auto unsorted_begin = std::next(first); unsorted_begin != last; ++unsorted_begin) {
101 for (auto curr = unsorted_begin; curr != first; --curr) {
102 auto prev = std::prev(curr);
103 if (!comp(*curr, *prev)) break;
104 std::iter_swap(prev, curr);
105 ++n_swaps;
106 }
107 }
108
109 return n_swaps;
110 }
111
123 template <std::ranges::forward_range Range, typename Compare = std::less<>>
124 std::size_t bubble_sort(Range &&rng, Compare comp = {}) { // NOLINT (ranges need not be forwarded)
125 return bubble_sort(std::ranges::begin(rng), std::ranges::end(rng), comp);
126 }
127
139 template <std::ranges::bidirectional_range Range, typename Compare = std::less<>>
140 std::size_t insertion_sort(Range &&rng, Compare comp = {}) { // NOLINT (ranges need not be forwarded)
141 return insertion_sort(std::ranges::begin(rng), std::ranges::end(rng), comp);
142 }
143
145
146} // namespace itertools
147
148#endif // _ITERTOOLS_SORT_HPP
std::size_t bubble_sort(ForwardIt first, ForwardIt last, Compare comp={})
Bubble sort elements in the given range.
Definition sort.hpp:58
std::size_t insertion_sort(BidirIt first, BidirIt last, Compare comp={})
Insertion sort elements in the given range.
Definition sort.hpp:95