generated from GauravWalia19/mernboilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmerge_sort.cpp
112 lines (95 loc) · 2.37 KB
/
merge_sort.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* PROBLEM: Merge Sort in C++
* AUTHOR: sourishphate
**/
#include <iostream>
#include <vector>
using namespace std;
/**
* This function merges two sorted halves of an array
*
* @param A: the array to be sorted
* @param low: starting index of the first half
* @param mid: ending index of the first half
* @param high: ending index of the second half
**/
void merge(vector<int>& A, int low, int mid, int high) {
int n1 = mid - low + 1; // Length of first half
int n2 = high - mid; // Length of second half
vector<int> L(n1); // Create temp arrays
vector<int> R(n2);
// Copy data to temporary arrays L[] and R[]
for (int i = 0; i < n1; i++) {
L[i] = A[low + i];
}
for (int j = 0; j < n2; j++) {
R[j] = A[mid + 1 + j];
}
int i = 0; // Initial index of first subarray
int j = 0; // Initial index of second subarray
int k = low; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
A[k] = L[i];
i++;
} else {
A[k] = R[j];
j++;
}
k++;
}
// Copy the remaining elements of L[], if any
while (i < n1) {
A[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of R[], if any
while (j < n2) {
A[k] = R[j];
j++;
k++;
}
}
/**
* This function sorts an array using merge sort algorithm
*
* @param A: the array to be sorted
* @param p: starting index
* @param r: ending index
**/
void mergeSort(vector<int>& A, int p, int r) {
if (p < r) {
int q = (p + r) / 2; // Find the midpoint
mergeSort(A, p, q); // Sort first half
mergeSort(A, q + 1, r); // Sort second half
merge(A, p, q, r); // Merge the two sorted halves
}
}
/**
* This function prints the array
*
* @param A: the array to print
**/
void printArray(const vector<int>& A) {
for (int val : A) {
cout << val << " ";
}
cout << endl;
}
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
vector<int> A(size);
cout << "Enter the elements of the array:\n";
for (int i = 0; i < size; i++) {
cin >> A[i];
}
cout << "Unsorted array: ";
printArray(A);
mergeSort(A, 0, A.size() - 1);
cout << "Sorted array: ";
printArray(A);
return 0;
}