forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cpp implementation of merge sort for NITSkmOS/Algorithms closes NITSkmOS#258
- Loading branch information
1 parent
40b7644
commit 0d24866
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <iostream> | ||
#include <cstdio> | ||
#include <vector> | ||
using namespace std; | ||
std::vector<int> vec; | ||
|
||
void join(std::vector<int>& arr, int, int, int); | ||
void sort(std::vector<int>& arr, int, int); | ||
void show(std::vector<int>& arr); | ||
|
||
int main() { | ||
int n = 10, a, i, arr[] = {2, 4, 3, 6, 5, 1, 9, 0, 8, 7}; | ||
// scanf("%d", &n); //For custom input | ||
// printf("Enter %ld number (space separated): ", n); //For custom inup | ||
for (i = 0; i < n; i++) { | ||
//scanf("%d", &a); | ||
vec.push_back(arr[i]); //vec.push_back(a); // For custom input | ||
} | ||
a = 0; | ||
sort(vec, a, n-1); | ||
show(vec); | ||
return 0; | ||
} | ||
|
||
void join(std::vector<int>& arr, int l, int m, int r) { | ||
// Merge operation of merge sort. | ||
int i, j, k; | ||
int s1 = m-l+1, s2 = r-m; | ||
int L[s1], R[s2]; | ||
for (i = 0; i < s1; i++) | ||
L[i] = arr[l+i]; | ||
for (j = 0; j < s2; j++) | ||
R[j] = arr[m+j+1]; | ||
i = 0; j = 0; k = l; | ||
while (i < s1 && j < s2) { | ||
if (L[i] < R[j]) { | ||
arr[k] = L[i]; | ||
i++; | ||
} else { | ||
arr[k] = R[j]; | ||
j++; | ||
} | ||
k++; | ||
} | ||
while (i < s1) { | ||
arr[k] = L[i]; i++; k++; | ||
} | ||
while (j < s2) { | ||
arr[k] = R[j]; j++; k++; | ||
} | ||
} | ||
|
||
void sort(std::vector<int>& arr, int l, int r) { | ||
// The major implementation of merge sort algorithm | ||
if (l < r) { | ||
int m = l+(r-l)/2; | ||
sort(arr, l, m); | ||
sort(arr, m+1, r); | ||
join(arr, l, m, r); | ||
} | ||
} | ||
|
||
void show(std::vector<int>& arr) { | ||
// A basic function to view vector values | ||
cout << "Sorted element using Merge Sort: "; | ||
std::vector<int>::iterator v = arr.begin(); | ||
while (v != arr.end()) { | ||
cout << *v <<" "; | ||
v++; | ||
} | ||
cout << endl; | ||
} |