Skip to content

Commit 8d4c734

Browse files
committed
added MergeSort.cpp and updated README.md
1 parent 3071f28 commit 8d4c734

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,8 +3150,8 @@ In order to achieve greater coverage and encourage more people to contribute to
31503150
</a>
31513151
</td>
31523152
<td> <!-- C++ -->
3153-
<a href="./CONTRIBUTING.md">
3154-
<img align="center" height="25" src="./logos/github.svg" />
3153+
<a href="./src/cpp/MergeSort.cpp">
3154+
<img align="center" height="25" src="./logos/cplusplus.svg" />
31553155
</a>
31563156
</td>
31573157
<td> <!-- Java -->

src/cpp/MergeSort.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
/**
5+
* Merge two sub-arrays of arr[]
6+
* - First sub-array is arr[left..mid]
7+
* - Second sub-array is arr[mid+1..right]
8+
*
9+
* @param arr The array to be sorted
10+
* @param left The left index of the first sub-array
11+
* @param mid The right index of the first sub-array
12+
* @param right The right index of the second sub-array
13+
*/
14+
15+
void merge(std::vector<int>& arr, int left, int mid, int right) {
16+
17+
int n1 = mid - left + 1; // size of the first sub-array.
18+
int n2 = right - mid; // size of the second sub-array.
19+
20+
// create temporary arrays to hold the data
21+
std::vector<int> leftSubarray(n1);
22+
std::vector<int> rightSubarray(n2);
23+
24+
// copy data to temporary arrays leftSubarray[] and rightSubarray[]
25+
for (int i = 0; i < n1; i++) {
26+
leftSubarray[i] = arr[left + i];
27+
}
28+
for (int j = 0; j < n2; j++) {
29+
rightSubarray[j] = arr[mid + 1 + j];
30+
}
31+
32+
// merge the two sub-arrays back into the original array arr[]
33+
int i = 0, j = 0, k = left;
34+
35+
while (i < n1 && j < n2) {
36+
if (leftSubarray[i] <= rightSubarray[j]) {
37+
arr[k] = leftSubarray[i];
38+
i++;
39+
}
40+
else {
41+
arr[k] = rightSubarray[j];
42+
j++;
43+
}
44+
k++;
45+
}
46+
47+
// copy the remaining elements of leftSubarray[], if any
48+
while (i < n1) {
49+
arr[k] = leftSubarray[i];
50+
i++;
51+
k++;
52+
}
53+
54+
// copy the remaining elements of rightSubarray[], if any
55+
while (j < n2) {
56+
arr[k] = rightSubarray[j];
57+
j++;
58+
k++;
59+
}
60+
}
61+
62+
63+
64+
/**
65+
* Merge Sort algorithm
66+
*
67+
* @param arr The array to be sorted
68+
* @param left The left index of the sub-array to be sorted
69+
* @param right The right index of the sub-array to be sorted
70+
*/
71+
72+
void mergeSort(std::vector<int>& arr, int left, int right) {
73+
if (left < right) {
74+
75+
int mid = left + (right - left) / 2; // calculate the middle index
76+
77+
// sort the first and second halves
78+
mergeSort(arr, left, mid);
79+
mergeSort(arr, mid + 1, right);
80+
81+
// merge the sorted halves
82+
merge(arr, left, mid, right);
83+
}
84+
}
85+
86+
int main() {
87+
88+
std::vector<int> arr = {12, 11, 13, 5, 6, 7, 9, 4, 10, 9, 11, 1, 11};
89+
90+
int arrSize = arr.size();
91+
92+
std::cout << "Original array: ";
93+
for (int num : arr) {
94+
std::cout << num << " ";
95+
}
96+
std::cout << std::endl;
97+
98+
mergeSort(arr, 0, arrSize - 1);
99+
100+
std::cout << "Sorted array: ";
101+
for (int num : arr) {
102+
std::cout << num << " ";
103+
}
104+
std::cout << std::endl;
105+
106+
return 0;
107+
}

0 commit comments

Comments
 (0)