Skip to main content

Didn't have enough reputation to comment onExtending the answer from @Clearer, but that answerwhich doesn't use the templated custom comparison class. Here is how I modified it to use it:

#include <algorithm>
#include <iterator>

template<class I, class C = std::less<typename std::iterator_traits<I>::value_type>>
void insertion_sort(I begin, I end, C comp = C()) {
    for (auto i = begin; i != end; ++i) {
        auto index = std::upper_bound(begin, i, *i, comp);
        std::rotate(index, i, i + 1);
    }
}

Didn't have enough reputation to comment on the answer from @Clearer, but that answer doesn't use the templated custom comparison class. Here is how I modified it to use it:

#include <algorithm>
#include <iterator>

template<class I, class C = std::less<typename std::iterator_traits<I>::value_type>>
void insertion_sort(I begin, I end, C comp = C()) {
    for (auto i = begin; i != end; ++i) {
        auto index = std::upper_bound(begin, i, *i, comp);
        std::rotate(index, i, i + 1);
    }
}

Extending the answer from @Clearer, which doesn't use the templated custom comparison class. Here is how I modified it to use it:

#include <algorithm>
#include <iterator>

template<class I, class C = std::less<typename std::iterator_traits<I>::value_type>>
void insertion_sort(I begin, I end, C comp = C()) {
    for (auto i = begin; i != end; ++i) {
        auto index = std::upper_bound(begin, i, *i, comp);
        std::rotate(index, i, i + 1);
    }
}
Source Link
dfreese
  • 131
  • 2

Didn't have enough reputation to comment on the answer from @Clearer, but that answer doesn't use the templated custom comparison class. Here is how I modified it to use it:

#include <algorithm>
#include <iterator>

template<class I, class C = std::less<typename std::iterator_traits<I>::value_type>>
void insertion_sort(I begin, I end, C comp = C()) {
    for (auto i = begin; i != end; ++i) {
        auto index = std::upper_bound(begin, i, *i, comp);
        std::rotate(index, i, i + 1);
    }
}