diff --git a/contributors.md b/contributors.md index 5dab382..f718353 100644 --- a/contributors.md +++ b/contributors.md @@ -8,4 +8,7 @@ 6. [Ujjval Patel](https://github.com/Ujjval-Patel) 7. [Shaguna Awasthi](https://github.com/Shagunaawasthi) 8. [Joy Banerjee](https://github.com/joybanerjee08) -9. [Kartikey Tripathi](https://github.com/kartikeytripathi) \ No newline at end of file +9. [Nikhil Arora](https://github.com/nikhilarora068) +10. [Ambika](https://github.com/Ambika55) +11. [Kartikey Tripathi](https://github.com/kartikeytripathi) +12. [Ananya Keshari](https://github.com/ana301) \ No newline at end of file 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); + } +}