25#ifndef MERGE_SORT_INCLUDED
26#define MERGE_SORT_INCLUDED
69template <
typename Element_type,
typename Comp_func>
70void insert_sort(Element_type **first, Element_type **last, Comp_func comp) {
71 for (Element_type **high_water_mark = first + 1; high_water_mark < last;
73 for (Element_type **cur = high_water_mark; cur > first; cur--) {
74 if (comp(*(cur - 1), *cur))
break;
76 Element_type *tmp = *(cur - 1);
101template <
typename Element_type,
typename Comp_func>
102void merge_sort(Element_type **first, Element_type **last, Comp_func comp) {
103 const uint elements =
static_cast<uint
>(last - first);
113 Element_type **middle = first + (elements) / 2;
118 std::queue<Element_type *> merged;
120 Element_type **cur1 = first;
121 Element_type **cur2 = middle;
123 for (uint i = 0; i < elements; i++) {
124 assert(cur1 < middle || cur2 < last);
127 merged.push(*cur2++);
128 else if (cur2 == last)
129 merged.push(*cur1++);
130 else if (comp(*cur1, *cur2))
131 merged.push(*cur1++);
133 merged.push(*cur2++);
136 Element_type **
result = first;
137 while (!merged.empty()) {
138 *
result++ = merged.front();
void insert_sort(Element_type **first, Element_type **last, Comp_func comp)
Sorts the elements in the range [first,last) into ascending order using insertion sort.
Definition: merge_sort.h:70
void merge_sort(Element_type **first, Element_type **last, Comp_func comp)
Sorts the elements in the range [first,last) into ascending order using merge sort.
Definition: merge_sort.h:102