1#ifndef ALGOPLUS_QUICK_SORT_H
2#define ALGOPLUS_QUICK_SORT_H
17template <
typename Iter> Iter median_of_three(Iter a, Iter b, Iter c) {
47template <
typename Iter>
void quick_sort(Iter begin, Iter end) {
48 auto distance = std::distance(begin, end);
54 Iter mid = begin + distance / 2;
55 Iter pivot_pos = median_of_three(begin, mid, end - 1);
56 auto pivot = *pivot_pos;
59 std::swap(*begin, *pivot_pos);
62 std::partition(begin + 1, end, [pivot](
const auto& elem) {
return elem < pivot; });
65 std::partition(middle1, end, [pivot](
const auto& elem) {
return !(pivot < elem); });
68 std::swap(*begin, *(middle1 - 1));
71 quick_sort(begin, middle1 - 1);
72 quick_sort(middle2, end);