2023-10-31 20:11:31 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace algo {
|
|
|
|
|
2023-11-01 09:19:25 +00:00
|
|
|
class MergeSort_v1 {
|
|
|
|
private:
|
|
|
|
template<typename Iterator, typename Comparator>
|
|
|
|
static auto merge(Iterator start, Iterator middle, Iterator end, Comparator cmp, Iterator output_start) -> void {
|
|
|
|
Iterator start_m = start;
|
|
|
|
Iterator begin = output_start;
|
|
|
|
Iterator start2 = middle + 1;
|
2023-10-31 20:11:31 +00:00
|
|
|
|
2023-11-01 09:19:25 +00:00
|
|
|
//merge from input until one half completes
|
|
|
|
while (start <= middle && start2 <= end) {
|
|
|
|
if (cmp(*start, *start2)) {
|
|
|
|
*output_start = *start;
|
|
|
|
start++;
|
|
|
|
} else {
|
|
|
|
*output_start = *start2;
|
|
|
|
start2++;
|
|
|
|
}
|
|
|
|
output_start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//try to finish first half
|
|
|
|
while (start <= middle) {
|
2023-10-31 20:11:31 +00:00
|
|
|
*output_start = *start;
|
|
|
|
start++;
|
2023-11-01 09:19:25 +00:00
|
|
|
output_start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (start2 <= end) {
|
2023-10-31 20:11:31 +00:00
|
|
|
*output_start = *start2;
|
|
|
|
start2++;
|
2023-11-01 09:19:25 +00:00
|
|
|
output_start++;
|
2023-10-31 20:11:31 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 09:19:25 +00:00
|
|
|
const auto size = std::distance(start_m, end);
|
|
|
|
for (auto i = 0; i <= size; i++, start_m++, begin++) {
|
|
|
|
*start_m = *begin;
|
|
|
|
}
|
2023-10-31 20:11:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-01 09:19:25 +00:00
|
|
|
template<typename Container, typename Iterator, typename Comparator>
|
|
|
|
static auto ms_split(Container &output_vec, Iterator start, Iterator end, Comparator cmp, Iterator output_start) -> void {
|
|
|
|
Iterator mid = start;
|
|
|
|
Iterator begin = output_start;
|
2023-10-31 20:11:31 +00:00
|
|
|
|
2023-11-01 09:19:25 +00:00
|
|
|
if (std::distance(start, end) < 1) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
//move mid iterator litterally to the mid
|
|
|
|
std::advance(mid, std::distance(start, end) / 2);
|
|
|
|
//sort the first half within an recursion
|
|
|
|
ms_split(output_vec, start, mid, cmp, output_start);
|
2023-10-31 20:11:31 +00:00
|
|
|
|
2023-11-01 09:19:25 +00:00
|
|
|
//move output iterator
|
|
|
|
std::advance(output_start, std::distance(start, mid + 1));
|
|
|
|
//sort the second half within a recursion
|
|
|
|
ms_split(output_vec, mid + 1, end, cmp, output_start);
|
2023-10-31 20:11:31 +00:00
|
|
|
|
2023-11-01 09:19:25 +00:00
|
|
|
//merge everything together starting from the complete beginning
|
|
|
|
merge(start, mid, end, cmp, begin);
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 20:11:31 +00:00
|
|
|
|
2023-11-01 09:19:25 +00:00
|
|
|
public:
|
|
|
|
template<typename Iterator, typename Comparator>
|
|
|
|
static auto sort(Iterator start, Iterator end, Comparator cmp) -> void {
|
|
|
|
using valtype = typename std::iterator_traits<Iterator>::value_type;
|
|
|
|
std::vector<valtype> temporary_dataset(std::distance(start, end));
|
|
|
|
ms_split(temporary_dataset, start, end - 1, cmp, temporary_dataset.begin());
|
2023-11-01 09:00:36 +00:00
|
|
|
}
|
2023-11-01 09:19:25 +00:00
|
|
|
};
|
2023-10-31 20:11:31 +00:00
|
|
|
|
|
|
|
}
|