Skip to content

Commit

Permalink
merge_sort.cpp: Add Cpp merge sort.
Browse files Browse the repository at this point in the history
Cpp implementation of merge sort for NITSkmOS/Algorithms

closes NITSkmOS#258
  • Loading branch information
yashasingh committed Oct 30, 2018
1 parent 40b7644 commit 0d24866
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This repository contains examples of various algorithms written on different pro
| Algorithm | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | [:octocat:](euclidean_gcd/Java) | [:octocat:](euclidean_gcd/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | |[:octocat:](merge_sort/Java) | [:octocat:](merge_sort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | [:octocat:](merge_sort/Cpp) |[:octocat:](merge_sort/Java) | [:octocat:](merge_sort/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | [:octocat:](quicksort/C) | | | [:octocat:](quicksort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) |[:octocat:](counting_sort/C) | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
Expand Down
72 changes: 72 additions & 0 deletions merge_sort/Cpp/merge_sort.cpp
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;
}

0 comments on commit 0d24866

Please sign in to comment.