Skip to content

Commit

Permalink
merge_sort.c: Add Merge Sort in C
Browse files Browse the repository at this point in the history
This adds Merge Sort algorithm written in C

Closes NITSkmOS#147
  • Loading branch information
123vivekr authored and sangamcse committed Oct 13, 2018
1 parent 013c77d commit 8d4d599
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This repository contains examples of various algorithms written on different pro
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | [:octocat:](euclidean_gcd/Java) | [:octocat:](euclidean_gcd/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | | | | [:octocat:](quicksort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | | | | [:octocat:](merge_sort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | | | [:octocat:](merge_sort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | [:octocat:](radix_sort/Cpp) | | [:octocat:](radix_sort/Python) |
Expand Down
73 changes: 73 additions & 0 deletions merge_sort/C/merge_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h> // for printf function

// Function prototypes
void mergesort(int[], int, int);
void merge(int[], int, int, int);

int main() {
int arr_size = 6;
int arr[6] = {10, 9, 8, 7, 6, 5};
mergesort(arr, 0, arr_size);

// Print sorted array
for (int i = 0; i < arr_size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

/*
* Recursively split the array and call the merge function
* Main entry point of Merge Sort
*/
void mergesort(int arr[], int l, int r) {
if(l >= r)
return;
int m = (l + r) / 2;
mergesort(arr, l, m);
mergesort(arr, m+1, r);
merge(arr, l, m, r);
}

// Merges the arrays in ascending order of elements
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], M[n2];

for(i = 0; i < n1; ++i)
L[i] = arr[l+i];
for(j = 0; j < n2; ++j)
M[j] = arr[m + 1 + j];

i = 0;
j = 0;
k = l;

while(i < n1 && j < n2) {
if(L[i] <= M[j]) {
arr[k] = L[i];
++i;
} else {
arr[k] = M[j];
++j;
}
++k;
}

while(i < n1) {
arr[k] = L[i];
++i;
++k;
}

while(j < n2) {
arr[k] = M[j];
++j;
++k;
}
}

0 comments on commit 8d4d599

Please sign in to comment.