Skip to content

Commit

Permalink
Couple of issues and areas for improvement.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pipinstallaadit authored Oct 15, 2024
1 parent e841605 commit 9b7a86a
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*
Expand All @@ -14,6 +14,7 @@
*
*/
#include <iostream>
#include <vector>

/**
*
Expand All @@ -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<int> 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 {
Expand All @@ -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++;
}
}

/**
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9b7a86a

Please sign in to comment.