From f5a305e12275150b4618a592465174da903170ed Mon Sep 17 00:00:00 2001 From: shruttipriya <55920115+shruttipriya@users.noreply.github.com> Date: Sun, 25 Oct 2020 01:37:51 +0530 Subject: [PATCH] Create sort a list of elements using the merge sort algorithm --- ...of elements using the merge sort algorithm | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 sort a list of elements using the merge sort algorithm diff --git a/sort a list of elements using the merge sort algorithm b/sort a list of elements using the merge sort algorithm new file mode 100644 index 0000000..c11c36b --- /dev/null +++ b/sort a list of elements using the merge sort algorithm @@ -0,0 +1,74 @@ +#include +/* Function to merge the two haves arra[l..m] and arra[m+1..r] of array arra[] */ + void merge(int arra[], int l, int m, int r) + { + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + /* create temp arrays */ + int L[n1], R[n2]; + /* Copy data to temp arrays L[] and R[] */ + for(i = 0; i < n1; i++) + L[i] = arra[l + i]; + for(j = 0; j < n2; j++) + R[j] = arra[m + 1+ j]; + i = 0; + j = 0; + k = l; + while (i < n1 && j < n2) + { + if (L[i] <= R[j]) + { + arra[k] = L[i]; + i++; + } + else + { + arra[k] = R[j]; + j++; + } + k++; + } + while (i < n1) + { + arra[k] = L[i]; + i++; + k++; + } + while (j < n2) + { + arra[k] = R[j]; + j++; + k++; + } + } + void mergeSort(int arra[], int l, int r) + { + if (l < r) + { + int m = l+(r-l)/2; //Same as (l+r)/2, but avoids overflow for large l and h + mergeSort(arra, l, m); + mergeSort(arra, m+1, r); + merge(arra, l, m, r); + } + } + /* Function to print an array */ + void print_array(int A[], int size) + { + int i; + for (i=0; i < size; i++) + printf("%d ", A[i]); + printf("\n"); + } + /* Test above functions */ + int main() + { + int arra[] = {125, 181, 130, 25, 61, 887}; + int arr_size = sizeof(arra)/sizeof(arra[0]); + printf("Given array is \n"); + print_array(arra, arr_size); + mergeSort(arra, 0, arr_size - 1); + printf("\nSorted array is \n"); + print_array(arra, arr_size); + return 0; + }