From 8fe868369be5e877c2fc032adc6dd9a6d38cff2e Mon Sep 17 00:00:00 2001 From: Akash Negi <55234838+NegiAkash890@users.noreply.github.com> Date: Wed, 26 May 2021 16:42:24 +0530 Subject: [PATCH] chore(CPlusPlus): created merge-sort (#324) * Created merge-sort.cpp * Updated README.md * Added Example and Fixed Bug --- algorithms/CPlusPlus/README.md | 1 + algorithms/CPlusPlus/Sorting/merge-sort.cpp | 66 +++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 algorithms/CPlusPlus/Sorting/merge-sort.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 2f7a9d03..bec91284 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -50,6 +50,7 @@ 10. [Radix Sort](Sorting/radix-sort.cpp) 11. [Shell Sort](Sorting/shell-sort.cpp) 12. [Binary Insertion Sort](Sorting/binary-insertion-sort.cpp) +13. [Merge Sort](Sorting/merge-sort.cpp) ## Strings 1. [Rabin-Karp pattern search algo](Strings/rabin-karp.cpp) diff --git a/algorithms/CPlusPlus/Sorting/merge-sort.cpp b/algorithms/CPlusPlus/Sorting/merge-sort.cpp new file mode 100644 index 00000000..276ccdc0 --- /dev/null +++ b/algorithms/CPlusPlus/Sorting/merge-sort.cpp @@ -0,0 +1,66 @@ +//Description : Merge Sort Algorithm +#include +using namespace std; + +void merging(int arr[], int l, int h, int m) { + int N1 = m - l + 1; //Left Array Size + int N2 = h - m; //Right Array Size + int left[N1]; + int right[N2]; + for (int i = 0; i < N1; i++) { + left[i] = arr[l + i]; //Copy elements to left array + } + for (int j = 0; j < N2; j++) { + right[j] = arr[m + 1 + j]; //Copy elements to right array + } + int i = 0; + int j = 0; + int k = l; + while (i < N1 && j < N2) { //Inserting back the elements from left and right array + if (left[i] <= right[j]) { //If left element is smaller than the right one then we push + arr[k++] = left[i++]; //left element to our main array(arr) + } else { + arr[k++] = right[j++]; //else we push right element to the main array + } + } + for (; i < N1; i++) { //These loop executes only when there is difference in size of + arr[k++] = left[i]; //left and right array + } + for (; j < N2; j++) { + arr[k++] = right[j]; + } + return; +} + +void merge_sort(int arr[], int l, int h) { + if (l < h) { + int mid = (l + ((h - l) / 2)); + merge_sort(arr, l, mid); //Recursively calling for elements before mid(including mid) + merge_sort(arr, mid + 1, h); //Recursively calling for elements after mid + merging(arr, l, h, mid); //Merging the two arrays + } + return; +} +int main() { + int arr[] = { 1,0,13,7,6,2,16 }; + cout << "Array Before Sorting" << endl; + for (int i = 0; i < 7; i++) { + cout << arr[i] << " "; + } + cout << endl; + merge_sort(arr, 0, 6); + cout << "Array After Sorting" << endl; + for (int i = 0; i < 7; i++) { + cout << arr[i] << " "; + } + return 0; + //Ex :Input : arr= { 1,0,13,7,6,2,16 }; + // Output + // Array Before Sorting + // 1 0 13 7 6 2 16 + // Array After Sorting + // 0 1 2 6 7 13 16 + //Time Complexity :O(nlogn) + //Auxiliary Space :O(n) + //where n is the array size +}