TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
merge_insertion_sort.cpp
Go to the documentation of this file.
1
10#include <algorithm>
11#include <array>
12#include <cassert>
13#include <ctime>
14#include <iostream>
15#include <memory>
16
20namespace sorting {
24namespace merge_insertion {
25
36template <typename T, size_t N>
37static void InsertionSort(std::array<T, N> *A, size_t start, size_t end) {
38 size_t i = 0, j = 0;
39 T *ptr = A->data();
40
41 for (i = start; i < end; i++) {
42 T temp = ptr[i];
43 j = i;
44 while (j > start && temp < ptr[j - 1]) {
45 ptr[j] = ptr[j - 1];
46 j--;
47 }
48 // for (j = i; j > start && temp < ptr[j - 1]; --j) {
49 // ptr[j] = ptr[j - 1];
50 // }
51
52 ptr[j] = temp;
53 }
54}
55
66template <typename T, size_t N>
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;
70
71 auto ptr = array->data();
72 std::array<T, N + 1> tempArray{0};
73
74 // While there are elements in the left or right runs
75 for (size_t index = min; index <= max; index++) {
76 // If left run head exists and is <= existing right run head.
77 if (firstIndex <= mid &&
78 (secondIndex > max || ptr[firstIndex] <= ptr[secondIndex])) {
79 tempArray[index] = ptr[firstIndex];
80 firstIndex++;
81 } else {
82 tempArray[index] = ptr[secondIndex];
83 secondIndex++;
84 }
85 }
86
87 // transfer to the initial array
88 memcpy(ptr + min, tempArray.data() + min, (max - min) * sizeof(T));
89 // for (int index = min; index <= max; index++) ptr[index] =
90 // tempArray[index];
91}
92
106template <typename T, size_t N>
107void mergeSort(std::array<T, N> *array, size_t min, size_t max,
108 size_t threshold) {
109 // prerequisite
110 if ((max - min) <= threshold) {
111 InsertionSort(array, min, max);
112 } else {
113 // get the middle point
114 size_t mid = (max + min) >> 1;
115
116 // apply merge sort to both parts of this
117 mergeSort(array, min, mid, threshold);
118 mergeSort(array, mid, max, threshold);
119
120 // and finally merge all that sorted stuff
121 merge(array, min, max, mid);
122 }
123}
124
125} // namespace merge_insertion
126} // namespace sorting
127
132static void test() {
133 constexpr size_t size = 30;
134 std::array<int, size> array{0};
135 // input
136 for (int i = 0; i < size; i++) {
137 array[i] = std::rand() % 100 - 50;
138 std::cout << array[i] << " ";
139 }
140 std::cout << std::endl;
141
142 sorting::merge_insertion::InsertionSort(&array, 0, size);
143 // sorting::merge_insertion::mergeSort(&array, 0, size, 10);
144
145 // output
146 for (int i = 0; i < size; ++i) {
147 std::cout << array[i] << " ";
148 }
149 std::cout << std::endl;
150
151 assert(std::is_sorted(std::begin(array), std::end(array)));
152 std::cout << "Test passed\n";
153}
154
159int main() {
160 std::srand(std::time(nullptr));
161 test();
162 return 0;
163}
static void InsertionSort(std::array< T, N > *A, size_t start, size_t end)
Insertion merge algorithm.
static void test()
Function to test code using random arrays.
int main()
Main function.
Combined Intersion-Merge sorting algorithm.
for working with vectors