22#ifndef _ITERTOOLS_SORT_HPP
23#define _ITERTOOLS_SORT_HPP
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;
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);
70 unsorted_end = last_swap;
94 template <std::b
idirectional_iterator B
idirIt,
typename Compare = std::less<>>
96 if (first == last)
return 0;
98 std::size_t n_swaps = 0;
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);
123 template <std::ranges::forward_range Range,
typename Compare = std::less<>>
125 return bubble_sort(std::ranges::begin(rng), std::ranges::end(rng), comp);
139 template <std::ranges::b
idirectional_range Range,
typename Compare = std::less<>>
141 return insertion_sort(std::ranges::begin(rng), std::ranges::end(rng), comp);
std::size_t bubble_sort(ForwardIt first, ForwardIt last, Compare comp={})
Bubble sort elements in the given range.
std::size_t insertion_sort(BidirIt first, BidirIt last, Compare comp={})
Insertion sort elements in the given range.