From 5cdd8334b8fc6788dd3f3aa7e6f7467d936730da Mon Sep 17 00:00:00 2001 From: DANISH <65593339+khand420@users.noreply.github.com> Date: Thu, 14 Oct 2021 12:22:03 +0530 Subject: [PATCH] Merge sort algorithm in c --- Programming/C/MergeSort/MergeSort.c | 78 +++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Programming/C/MergeSort/MergeSort.c diff --git a/Programming/C/MergeSort/MergeSort.c b/Programming/C/MergeSort/MergeSort.c new file mode 100644 index 0000000..5359e16 --- /dev/null +++ b/Programming/C/MergeSort/MergeSort.c @@ -0,0 +1,78 @@ +#include + + // MergeSort by Khand + +void printArray(int *A, int n) +{ + for (int i = 0; i < n; i++) + { + printf("%d ", A[i]); + } + printf("\n"); +} + +void merge(int A[], int mid, int low, int high) +{ + int i, j, k, B[100]; + i = low; + j = mid + 1; + k = low; + + while (i <= mid && j <= high) + { + if (A[i] < A[j]) + { + B[k] = A[i]; + i++; + k++; + } + else + { + B[k] = A[j]; + j++; + k++; + } + } + while (i <= mid) + { + B[k] = A[i]; + k++; + i++; + } + while (j <= high) + { + B[k] = A[j]; + k++; + j++; + } + for (int i = low; i <= high; i++) + { + A[i] = B[i]; + } + +} + +void mergeSort(int A[], int low, int high){ + int mid; + if(low