From 64cd25e69d0133d5026cbaa6faff7495f3f87b67 Mon Sep 17 00:00:00 2001 From: yashasingh Date: Tue, 30 Oct 2018 18:31:41 +0530 Subject: [PATCH] merge_sort.cpp: Add Cpp merge sort Cpp implementation of merge sort for NITSkmOS/Algorithms closes https://github.com/NITSkmOS/Algorithms/issues/258 --- README.md | 2 +- merge_sort/Cpp/merge_sort.cpp | 72 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 merge_sort/Cpp/merge_sort.cpp diff --git a/README.md b/README.md index d8f56d17..0c94e2fe 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,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) | diff --git a/merge_sort/Cpp/merge_sort.cpp b/merge_sort/Cpp/merge_sort.cpp new file mode 100644 index 00000000..720effc2 --- /dev/null +++ b/merge_sort/Cpp/merge_sort.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; +std::vector vec; + +void join(std::vector& arr, int, int, int); +void sort(std::vector& arr, int, int); +void show(std::vector& arr); + +int main() { + int n = 10, a, i, arr[] = {2, 4, 3, 6, 5, 1, 9, 0, 8, 7}; + // cin>>n; //For custom input + // cout<<"Enter "<>a; //For custom input + 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& 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& 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& arr) { + // A basic function to view vector values + cout << "Sorted element using Merge Sort: "; + std::vector::iterator v = arr.begin(); + while (v != arr.end()) { + cout << *v <<" "; + v++; + } + cout << endl; +}