From db4295745c3420d77ff8213e60e978f133ee2c37 Mon Sep 17 00:00:00 2001 From: ana301 <31302345+ana301@users.noreply.github.com> Date: Tue, 2 Oct 2018 00:06:18 +0530 Subject: [PATCH 1/2] Update contributors.md --- contributors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributors.md b/contributors.md index eb4db77..535a8eb 100644 --- a/contributors.md +++ b/contributors.md @@ -8,4 +8,4 @@ 6. [Ujjval Patel](https://github.com/Ujjval-Patel) 7. [Shaguna Awasthi](https://github.com/Shagunaawasthi) 8. [Joy Banerjee](https://github.com/joybanerjee08) -9. [Your Name](https://github.com/yourprofile) +9. [Ananya Keshari](https://github.com/ana301) From 90744a0a2be841c4ebd63c42313582c24ba31984 Mon Sep 17 00:00:00 2001 From: ana301 <31302345+ana301@users.noreply.github.com> Date: Tue, 2 Oct 2018 00:08:49 +0530 Subject: [PATCH 2/2] Create mergesort.cpp --- sorting/mergesort.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 sorting/mergesort.cpp diff --git a/sorting/mergesort.cpp b/sorting/mergesort.cpp new file mode 100644 index 0000000..cc04da2 --- /dev/null +++ b/sorting/mergesort.cpp @@ -0,0 +1,63 @@ + + +void merge(int arr[], int l, int m, int r) +{ + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + + int L[n1], R[n2]; + + for (i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1+ j]; + + i = 0; // Initial index of first subarray + j = 0; // Initial index of second subarray + k = l; // Initial index of merged subarray + while (i < n1 && j < n2) + { + if (L[i] <= R[j]) + { + arr[k] = L[i]; + i++; + } + else + { + arr[k] = R[j]; + j++; + } + k++; + } + + + while (i < n1) + { + arr[k] = L[i]; + i++; + k++; + } + + + while (j < n2) + { + arr[k] = R[j]; + j++; + k++; + } +} +{ + if (l < r) + { + // Same as (l+r)/2, but avoids overflow for + // large l and h + int m = l+(r-l)/2; + + // Sort first and second halves + mergeSort(arr, l, m); + mergeSort(arr, m+1, r); + + merge(arr, l, m, r); + } +}