67static void merge(std::array<T, N> *array,
size_t min,
size_t max,
size_t mid) {
68 size_t firstIndex = min;
69 size_t secondIndex = mid + 1;
71 auto ptr = array->data();
72 std::array<T, N + 1> tempArray{0};
75 for (
size_t index = min; index <= max; index++) {
77 if (firstIndex <= mid &&
78 (secondIndex > max || ptr[firstIndex] <= ptr[secondIndex])) {
79 tempArray[index] = ptr[firstIndex];
82 tempArray[index] = ptr[secondIndex];
88 memcpy(ptr + min, tempArray.data() + min, (max - min) *
sizeof(T));
107void mergeSort(std::array<T, N> *array,
size_t min,
size_t max,
110 if ((max - min) <= threshold) {
114 size_t mid = (max + min) >> 1;
117 mergeSort(array, min, mid, threshold);
118 mergeSort(array, mid, max, threshold);
121 merge(array, min, max, mid);
133 constexpr size_t size = 30;
134 std::array<int, size> array{0};
136 for (
int i = 0; i < size; i++) {
137 array[i] = std::rand() % 100 - 50;
138 std::cout << array[i] <<
" ";
140 std::cout << std::endl;
142 sorting::merge_insertion::InsertionSort(&array, 0, size);
146 for (
int i = 0; i < size; ++i) {
147 std::cout << array[i] <<
" ";
149 std::cout << std::endl;
151 assert(std::is_sorted(std::begin(array), std::end(array)));
152 std::cout <<
"Test passed\n";