Skip to content

Commit 9b7a86a

Browse files
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.
1 parent e841605 commit 9b7a86a

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

sorting/merge_sort.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* \addtogroup sorting Sorting Algorithms
33
* @{
44
* \file
5-
* \brief [Merege Sort Algorithm
6-
* (MEREGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation
5+
* \brief [Merge Sort Algorithm
6+
* (MERGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation
77
*
88
* \author [Ayaan Khan](http://github.com/ayaankhan98)
99
*
@@ -14,6 +14,7 @@
1414
*
1515
*/
1616
#include <iostream>
17+
#include <vector>
1718

1819
/**
1920
*
@@ -31,20 +32,18 @@
3132
* @param r - end index or right index of second half array
3233
*/
3334
void merge(int *arr, int l, int m, int r) {
34-
int i, j, k;
3535
int n1 = m - l + 1;
3636
int n2 = r - m;
3737

38-
int *L = new int[n1], *R = new int[n2];
38+
std::vector<int> L(n1), R(n2);
3939

40-
for (i = 0; i < n1; i++) L[i] = arr[l + i];
41-
for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
40+
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
41+
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
4242

43-
i = 0;
44-
j = 0;
45-
k = l;
46-
while (i < n1 || j < n2) {
47-
if (j >= n2 || (i < n1 && L[i] <= R[j])) {
43+
int i = 0, j = 0, k = l;
44+
45+
while (i < n1 && j < n2) {
46+
if (L[i] <= R[j]) {
4847
arr[k] = L[i];
4948
i++;
5049
} else {
@@ -54,8 +53,17 @@ void merge(int *arr, int l, int m, int r) {
5453
k++;
5554
}
5655

57-
delete[] L;
58-
delete[] R;
56+
while (i < n1) {
57+
arr[k] = L[i];
58+
i++;
59+
k++;
60+
}
61+
62+
while (j < n2) {
63+
arr[k] = R[j];
64+
j++;
65+
k++;
66+
}
5967
}
6068

6169
/**
@@ -89,15 +97,22 @@ void show(int *arr, int size) {
8997
/** Main function */
9098
int main() {
9199
int size;
92-
std::cout << "Enter the number of elements : ";
100+
std::cout << "Enter the number of elements: ";
93101
std::cin >> size;
102+
103+
if (size <= 0) {
104+
std::cout << "Invalid size.\n";
105+
return 1;
106+
}
107+
94108
int *arr = new int[size];
95-
std::cout << "Enter the unsorted elements : ";
109+
std::cout << "Enter the unsorted elements: ";
96110
for (int i = 0; i < size; ++i) {
97111
std::cin >> arr[i];
98112
}
113+
99114
mergeSort(arr, 0, size - 1);
100-
std::cout << "Sorted array : ";
115+
std::cout << "Sorted array: ";
101116
show(arr, size);
102117
delete[] arr;
103118
return 0;

0 commit comments

Comments
 (0)