Skip to content

Commit 2a8f02e

Browse files
committed
Added Merge Sort implementation in C (Fixes #212)
1 parent 636ef0d commit 2a8f02e

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

C/algorithms/sorting/merge_sort.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
/*
3+
* Algorithm: Merge Sort
4+
* Description: A divide-and-conquer sorting algorithm that recursively splits the array, sorts the halves, and merges them in order.
5+
* Time Complexity: O(n log n) for best, average, and worst cases
6+
* Space Complexity: O(n) due to temporary arrays used during merging
7+
* Author: Ayush Anand
8+
* Date: 2025-10-01
9+
*/
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
14+
// Function to merge two sorted subarrays
15+
void merge(int arr[], int left, int mid, int right) {
16+
int n1 = mid - left + 1; // Size of left subarray
17+
int n2 = right - mid; // Size of right subarray
18+
19+
// Dynamically allocate temporary arrays
20+
int *L = (int *)malloc(n1 * sizeof(int));
21+
int *R = (int *)malloc(n2 * sizeof(int));
22+
23+
if (L == NULL || R == NULL) {
24+
printf("Memory allocation failed!\n");
25+
exit(1);
26+
}
27+
28+
// Copy data to temporary arrays L[] and R[]
29+
for (int i = 0; i < n1; i++)
30+
L[i] = arr[left + i];
31+
for (int j = 0; j < n2; j++)
32+
R[j] = arr[mid + 1 + j];
33+
34+
// Merge the temporary arrays back into arr[left..right]
35+
int i = 0, j = 0, k = left;
36+
37+
while (i < n1 && j < n2) {
38+
if (L[i] <= R[j]) {
39+
arr[k++] = L[i++];
40+
} else {
41+
arr[k++] = R[j++];
42+
}
43+
}
44+
45+
// Copy remaining elements, if any
46+
while (i < n1)
47+
arr[k++] = L[i++];
48+
while (j < n2)
49+
arr[k++] = R[j++];
50+
51+
// Free dynamically allocated memory
52+
free(L);
53+
free(R);
54+
}
55+
56+
// Recursive function to implement Merge Sort
57+
void mergeSort(int arr[], int left, int right) {
58+
if (left < right) {
59+
int mid = left + (right - left) / 2;
60+
61+
// Recursively sort first and second halves
62+
mergeSort(arr, left, mid);
63+
mergeSort(arr, mid + 1, right);
64+
65+
// Merge the sorted halves
66+
merge(arr, left, mid, right);
67+
}
68+
}
69+
70+
// Function to print an array
71+
void printArray(int arr[], int size) {
72+
for (int i = 0; i < size; i++)
73+
printf("%d ", arr[i]);
74+
printf("\n");
75+
}
76+
77+
// Driver code
78+
int main() {
79+
int n;
80+
81+
printf("Enter the number of elements: ");
82+
scanf("%d", &n);
83+
84+
int *arr = (int *)malloc(n * sizeof(int));
85+
if (arr == NULL) {
86+
printf("Memory allocation failed!\n");
87+
return 1;
88+
}
89+
90+
printf("Enter %d elements:\n", n);
91+
for (int i = 0; i < n; i++)
92+
scanf("%d", &arr[i]);
93+
94+
printf("Original array: ");
95+
printArray(arr, n);
96+
97+
mergeSort(arr, 0, n - 1);
98+
99+
printf("Sorted array: ");
100+
printArray(arr, n);
101+
102+
free(arr); // Free dynamically allocated memory
103+
return 0;
104+
}

0 commit comments

Comments
 (0)