cp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub Ebishu-0309/cp-library

:warning: merge_sort.hpp

Code

template <class Iterator, class Compare>
void merge_sort(Iterator first, Iterator last, Compare comp) {
    const auto d = last - first;
    if (d <= 1) return;
    if (d == 2) {
        if (!comp(*first, *(first + 1))) swap(*first, *(first + 1));
        return;
    }
    merge_sort(first, first + d / 2, comp);
    merge_sort(first + d / 2, last, comp);
    inplace_merge(first, first + d / 2, last, comp);
}
#line 1 "merge_sort.hpp"
template <class Iterator, class Compare>
void merge_sort(Iterator first, Iterator last, Compare comp) {
    const auto d = last - first;
    if (d <= 1) return;
    if (d == 2) {
        if (!comp(*first, *(first + 1))) swap(*first, *(first + 1));
        return;
    }
    merge_sort(first, first + d / 2, comp);
    merge_sort(first + d / 2, last, comp);
    inplace_merge(first, first + d / 2, last, comp);
}
Back to top page