-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCS_17_RecursiveMergeSort.cpp
65 lines (61 loc) · 1.5 KB
/
CS_17_RecursiveMergeSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
void merge(vector<int> &arr, int low, int mid, int high)
{
vector<int> temp;
// [left...mid]
// [mid+1...high]
int left = low;
int right = mid + 1;
while (left <= mid && right <= high)
{
if (arr[left] <= arr[right])
{
temp.push_back(arr[left]);
left++;
}
else
{
temp.push_back(arr[right]);
right++;
}
}
// if there are still some elements left in either of the arrays to be merged
while (left <= mid)
{
temp.push_back(arr[left]);
left++;
}
while (right <= high)
{
temp.push_back(arr[right]);
right++;
}
// Now copy the elements from temp to arr
for (int i = low; i <= high; i++)
{
arr[i] = temp[i - low]; // i-low because temp starts from 0 and arr starts from low index (not 0) in the original array (arr)
}
}
void mergeSort(vector<int> &arr, int low, int high)
{
if (low == high)
return;
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
int main()
{
vector<int> arr = {64, 25, 12, 22, 11};
cout << "Array before sorting: ";
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
int n = arr.size();
mergeSort(arr, 0, n - 1);
cout << "\nArray after sorting: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}