From b06bbf4dc6c46a3284d7852bb570438384eef4ef Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Sat, 20 Jun 2020 21:00:52 +0530 Subject: [PATCH] Fixed Bug [munmap_chunck() core dumped] --- sorting/merge_sort.cpp | 72 +++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index 82ab869cd..c28a28fe6 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -1,5 +1,36 @@ +/** + * \addtogroup sorting Sorting Algorithms + * @file{ + * \file + * \breif [Merege Sort Algorithm + * (MEREGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation + * + * \author [Ayaan Khan] (http://github.com/ayaankhan98) + * + * Merge Sort is an efficient, general purpose, comparison + * based sorting algorithm. + * Merge Sort is a divide and conquer algorithm + * + */ + #include +/** + * + * The merge() function is used for merging two halves. + * The merge(arr, l, m, r) is key process that assumes that + * arr[l..m] and arr[m+1..r] are sorted and merges the two + * sorted sub-arrays into one. + * + * @param arr[] is the array with two halves one is arr[l...m] and + * other is arr[m+1...l] + * @param l is the left index of first half array + * @param m is the end index of right index of first half array + * + * (The second array starts form m+1 and goes till l) + * + * @param l is the end index of right index of second half array + */ void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; @@ -40,42 +71,47 @@ void merge(int arr[], int l, int m, int r) { delete[] R; } +/** + * Merge sort is a divide and conquer algorithm, it divides the + * input array into two halves and calls itself for the two halves + * and then calls merge() to merge the two halves + * + * @param arr[] the array which is to be sorted + * @param l define the left index of array + * @param r defines the right index of array + * + */ void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; - mergeSort(arr, l, m); mergeSort(arr, m + 1, r); - merge(arr, l, m, r); } } -void show(int A[], int size) { - int i; - for (i = 0; i < size; i++) std::cout << A[i] << "\n"; +/** + * A simple utility function used to print the array after + * sorting + */ +void show(int arr[], int size) { + for (int i = 0; i < size; i++) std::cout << arr[i] << " "; + std::cout << "\n"; } +/** Main function */ int main() { int size; - std::cout << "\nEnter the number of elements : "; - + std::cout << "Enter the number of elements : "; std::cin >> size; - int *arr = new int[size]; - - std::cout << "\nEnter the unsorted elements : "; - + std::cout << "Enter the unsorted elements : "; for (int i = 0; i < size; ++i) { - std::cout << "\n"; std::cin >> arr[i]; } - - mergeSort(arr, 0, size); - - std::cout << "Sorted array\n"; - show(arr, size); - + mergeSort(arr, 0, size-1); + std::cout << "Sorted array : "; + show(arr, size-1); delete[] arr; return 0; }