41 template<
typename ForwardIter,
typename BinaryPred>
42 void selection_sort(ForwardIter first, ForwardIter last,
const BinaryPred& LT) {
43 for (
auto it = first; it != last; it++)
44 std::swap(*it, *std::min_element(it, last, LT));
74 template<
typename ForwardIter>
76 selection_sort(first, last, std::less<typename ForwardIter::value_type>());
81 template<
typename RandomAccessIter,
typename BinaryPred>
83 RandomAccessIter last,
84 const BinaryPred& LT) {
89 int sz = std::distance(first, last);
91 RandomAccessIter it = first + rand() % sz;
92 std::swap(*it, *(last - 1));
93 auto pivot = last - 1;
95 RandomAccessIter sep = first;
96 for (
auto it = first; it != last; it++) {
102 std::swap(*pivot, *sep);
106 template<
typename RandomAccessIter,
typename BinaryPred>
107 void quick_sort(RandomAccessIter first, RandomAccessIter last,
108 const BinaryPred& LT) {
117 template<
typename RandomAccessIter>
118 void quick_sort(RandomAccessIter first, RandomAccessIter last) {
119 quick_sort(first, last, std::less<typename RandomAccessIter::value_type>());
123 template<
typename RandomAccessIter,
typename BinaryPred>
124 void merge_sort(RandomAccessIter first, RandomAccessIter last,
125 const BinaryPred& LT) {
126 int sz = std::distance(first, last);
128 std::vector<typename RandomAccessIter::value_type>
129 subarr1(first, first + sz / 2);
130 std::vector<typename RandomAccessIter::value_type>
131 subarr2(first + sz / 2, last);
132 merge_sort(subarr1.begin(), subarr1.end(), LT);
133 merge_sort(subarr2.begin(), subarr2.end(), LT);
134 std::merge(subarr1.begin(), subarr1.end(),
135 subarr2.begin(), subarr2.end(), first);
139 template<
typename RandomAccessIter>
140 void merge_sort(RandomAccessIter first, RandomAccessIter last) {
141 merge_sort(first, last, std::less<typename RandomAccessIter::value_type>());
145 template<
typename RandomAccessIter,
typename BinaryPred>
146 void heap_sort(RandomAccessIter first, RandomAccessIter last,
147 const BinaryPred& LT) {
149 typename std::iterator_traits<RandomAccessIter>::value_type val_type;
153 for (RandomAccessIter it = first; it != last; it++)
156 for (RandomAccessIter it = first; it != last; it++) {
163 template<
typename RandomAccessIter>
164 void heap_sort(RandomAccessIter first, RandomAccessIter last) {
165 heap_sort(first, last, std::less<typename RandomAccessIter::value_type>());
void delete_min()
Definition: binary_heap.h:224
void quick_sort(RandomAccessIter first, RandomAccessIter last, const BinaryPred <)
Definition: sort.h:107
void insert(const elt_t &elt)
Definition: binary_heap.h:218
Definition: binary_heap.h:213
void selection_sort(ForwardIter first, ForwardIter last, const BinaryPred <)
Definition: sort.h:42
const elt_t & get_min() const
Definition: binary_heap.h:221
RandomAccessIter randomized_partition(RandomAccessIter first, RandomAccessIter last, const BinaryPred <)
Definition: sort.h:82
void heap_sort(RandomAccessIter first, RandomAccessIter last, const BinaryPred <)
Definition: sort.h:146
void merge_sort(RandomAccessIter first, RandomAccessIter last, const BinaryPred <)
Definition: sort.h:124