From 9b7a86ae9ce9795ebadcbae6a78246f521fad4a6 Mon Sep 17 00:00:00 2001 From: pipinstallaadit <34955763+pipinstallaadit@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:11:53 +0530 Subject: [PATCH] Couple of issues and areas for improvement. 1. Typographical Error in Doxygen Comment In the Doxygen comment at the beginning of the file, "Merege Sort" should be corrected to "Merge Sort". 2. Merging Logic In the merge function, the merging logic can lead to out-of-bounds access if both sub-arrays have been completely traversed. The condition in the while loop should ensure that you only access elements of L and R if they are within bounds: 3. Memory Management Using new and delete[] for the temporary arrays (L and R) is fine, but you can also use std::vector from the C++ Standard Library to simplify memory management: 4. Input Validation You should consider adding input validation when reading the number of elements and the actual elements to avoid undefined behavior. 5. Main Function Improvements You can also enhance the main function by encapsulating the input logic in a separate function, improving readability. --- sorting/merge_sort.cpp | 47 ++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index 74087491bef..3956319e6af 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -2,8 +2,8 @@ * \addtogroup sorting Sorting Algorithms * @{ * \file - * \brief [Merege Sort Algorithm - * (MEREGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation + * \brief [Merge Sort Algorithm + * (MERGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation * * \author [Ayaan Khan](http://github.com/ayaankhan98) * @@ -14,6 +14,7 @@ * */ #include +#include /** * @@ -31,20 +32,18 @@ * @param r - end index or right index of second half array */ void merge(int *arr, int l, int m, int r) { - int i, j, k; int n1 = m - l + 1; int n2 = r - m; - int *L = new int[n1], *R = new int[n2]; + std::vector L(n1), R(n2); - for (i = 0; i < n1; i++) L[i] = arr[l + i]; - for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; + for (int i = 0; i < n1; i++) L[i] = arr[l + i]; + for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; - i = 0; - j = 0; - k = l; - while (i < n1 || j < n2) { - if (j >= n2 || (i < n1 && L[i] <= R[j])) { + int i = 0, j = 0, k = l; + + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { @@ -54,8 +53,17 @@ void merge(int *arr, int l, int m, int r) { k++; } - delete[] L; - delete[] R; + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } } /** @@ -89,15 +97,22 @@ void show(int *arr, int size) { /** Main function */ int main() { int size; - std::cout << "Enter the number of elements : "; + std::cout << "Enter the number of elements: "; std::cin >> size; + + if (size <= 0) { + std::cout << "Invalid size.\n"; + return 1; + } + int *arr = new int[size]; - std::cout << "Enter the unsorted elements : "; + std::cout << "Enter the unsorted elements: "; for (int i = 0; i < size; ++i) { std::cin >> arr[i]; } + mergeSort(arr, 0, size - 1); - std::cout << "Sorted array : "; + std::cout << "Sorted array: "; show(arr, size); delete[] arr; return 0;