From be968afe3c984f09b1a6d6f168ca9634cd05597a Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Thu, 27 Aug 2020 16:27:07 -0400 Subject: [PATCH] working code --- sorting/merge_insertion_sort.cpp | 216 ++++++++++++++++++++----------- 1 file changed, 141 insertions(+), 75 deletions(-) diff --git a/sorting/merge_insertion_sort.cpp b/sorting/merge_insertion_sort.cpp index 4cc7de3a3..b38c86839 100644 --- a/sorting/merge_insertion_sort.cpp +++ b/sorting/merge_insertion_sort.cpp @@ -1,90 +1,156 @@ -#include -using namespace std; -void InsertionSort(int *A, int n) -{ - int i, temp, j; - for (i = 0; i < n; i++) { +/** + * @file + * @author [@sinkyoungdeok](https://github.com/sinkyoungdeok) + * @author [Krishna Vedala](https://github.com/kvedala) + * @brief + */ +#include +#include +#include +#include - temp = A[i]; - j = i; +/** + * @brief + * + */ +namespace sorting { +/** + * @brief + * + */ +namespace merge_insertion { - while (j > 0 && temp < A[j - 1]) { - A[j] = A[j - 1]; - j = j - 1; +/** + * @brief + * + * @tparam T + * @tparam N + * @param A + * @param start + * @param end + */ +template +void InsertionSort(std::array *A, size_t start, size_t end) { + size_t i, j; + T *ptr = A->data(); - } - A[j] = temp; - } -} -void merge(int array[], int min, int max, int mid); -void mergeSort(int array[], int min, int max, int threshold) -{ - // prerequisite - if ((max - min + 1) <= threshold) - { - InsertionSort(&array[min], max); - } - else - { - // get the middle point - int mid = (max + min) / 2; + for (i = start; i < end; i++) { + T temp = ptr[i]; + j = i; + while (j > start && temp < ptr[j - 1]) { + ptr[j] = ptr[j - 1]; + j--; + } + // for (j = i; j > start && temp < ptr[j - 1]; --j) { + // ptr[j] = ptr[j - 1]; + // } - // apply merge sort to both parts of this - mergeSort(array, min, mid, threshold); - mergeSort(array, mid + 1, max, threshold); - - // and finally merge all that sorted stuff - merge(array, min, max, mid); - } + ptr[j] = temp; + } } -void merge(int array[], int min, int max, int mid) -{ - int firstIndex = min; - int secondIndex = mid + 1; - int * tempArray = new int[max + 1]; +/** + * @brief + * + * @tparam T + * @tparam N + * @param array + * @param min + * @param max + * @param mid + */ +template +void merge(std::array *array, size_t min, size_t max, size_t mid) { + size_t firstIndex = min; + size_t secondIndex = mid + 1; + auto ptr = array->data(); + auto tempArray = std::unique_ptr(new T[max + 1]); - // While there are elements in the left or right runs - for (int index = min; index <= max; index++) { - // If left run head exists and is <= existing right run head. - if (firstIndex <= mid && (secondIndex > max || array[firstIndex] <= array[secondIndex])) - { - tempArray[index] = array[firstIndex]; - firstIndex = firstIndex + 1; - } + // While there are elements in the left or right runs + for (size_t index = min; index <= max; index++) { + // If left run head exists and is <= existing right run head. + if (firstIndex <= mid && + (secondIndex > max || ptr[firstIndex] <= ptr[secondIndex])) { + tempArray[index] = ptr[firstIndex]; + firstIndex++; + } - else - { - tempArray[index] = array[secondIndex]; - secondIndex = secondIndex + 1; - } + else { + tempArray[index] = ptr[secondIndex]; + secondIndex++; + } + } - } - - // transfer to the initial array - for (int index = min; index <= max; index++) - array[index] = tempArray[index]; + // transfer to the initial array + memcpy(ptr + min, tempArray.get() + min, (max - min) * sizeof(T)); + // for (int index = min; index <= max; index++) ptr[index] = + // tempArray[index]; } -int main() -{ - int size = 10; - int *_array = new int[size]; - //input - for (int i = 0; i < size; i++) - { - cin >> _array[i]; - } - mergeSort(_array, 0, size ,20); +/** + * @brief + * + * @tparam T + * @tparam N + * @param array + * @param min + * @param max + * @param threshold + */ +template +void mergeSort(std::array *array, size_t min, size_t max, + size_t threshold) { + // prerequisite + if ((max - min) <= threshold) { + InsertionSort(array, min, max); + } else { + // get the middle point + size_t mid = (max + min) >> 1; - //output - for (int i = 0; i < size; ++i) - { - cout << _array[i] << "\t"; - } + // apply merge sort to both parts of this + mergeSort(array, min, mid, threshold); + mergeSort(array, mid, max, threshold); - - delete[] _array; - return 0; + // and finally merge all that sorted stuff + merge(array, min, max, mid); + } +} + +} // namespace merge_insertion +} // namespace sorting + +/** + * @brief + * + */ +void test() { + constexpr size_t size = 30; + std::array array{0}; + // input + for (int i = 0; i < size; i++) { + array[i] = std::rand() % 100 - 50; + std::cout << array[i] << " "; + } + std::cout << std::endl; + + sorting::merge_insertion::InsertionSort(&array, 0, size); + // sorting::merge_insertion::mergeSort(&array, 0, size, 10); + + // output + for (int i = 0; i < size; ++i) { + std::cout << array[i] << " "; + } + std::cout << std::endl; +} + +/** + * @brief + * + * @return int + */ +int main() { + std::srand(std::time(nullptr)); + test(); + return 0; }