Skip to content

Commit f1eddf4

Browse files
Couple of issues and areas for improvement. (TheAlgorithms#2841)
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. Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
1 parent 93a700c commit f1eddf4

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
*
@@ -17,6 +17,7 @@
1717
* In built-in sort function merge sort along with quick sort is used.
1818
*/
1919
#include <iostream>
20+
#include <vector>
2021

2122
/**
2223
*
@@ -34,20 +35,18 @@
3435
* @param r - end index or right index of second half array
3536
*/
3637
void merge(int *arr, int l, int m, int r) {
37-
int i, j, k;
3838
int n1 = m - l + 1;
3939
int n2 = r - m;
4040

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

43-
for (i = 0; i < n1; i++) L[i] = arr[l + i];
44-
for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
43+
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
44+
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
4545

46-
i = 0;
47-
j = 0;
48-
k = l;
49-
while (i < n1 || j < n2) {
50-
if (j >= n2 || (i < n1 && L[i] <= R[j])) {
46+
int i = 0, j = 0, k = l;
47+
48+
while (i < n1 && j < n2) {
49+
if (L[i] <= R[j]) {
5150
arr[k] = L[i];
5251
i++;
5352
} else {
@@ -57,8 +56,17 @@ void merge(int *arr, int l, int m, int r) {
5756
k++;
5857
}
5958

60-
delete[] L;
61-
delete[] R;
59+
while (i < n1) {
60+
arr[k] = L[i];
61+
i++;
62+
k++;
63+
}
64+
65+
while (j < n2) {
66+
arr[k] = R[j];
67+
j++;
68+
k++;
69+
}
6270
}
6371

6472
/**
@@ -92,15 +100,22 @@ void show(int *arr, int size) {
92100
/** Main function */
93101
int main() {
94102
int size;
95-
std::cout << "Enter the number of elements : ";
103+
std::cout << "Enter the number of elements: ";
96104
std::cin >> size;
105+
106+
if (size <= 0) {
107+
std::cout << "Invalid size.\n";
108+
return 1;
109+
}
110+
97111
int *arr = new int[size];
98-
std::cout << "Enter the unsorted elements : ";
112+
std::cout << "Enter the unsorted elements: ";
99113
for (int i = 0; i < size; ++i) {
100114
std::cin >> arr[i];
101115
}
116+
102117
mergeSort(arr, 0, size - 1);
103-
std::cout << "Sorted array : ";
118+
std::cout << "Sorted array: ";
104119
show(arr, size);
105120
delete[] arr;
106121
return 0;

0 commit comments

Comments
 (0)