-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram_67.c
More file actions
58 lines (47 loc) · 1.35 KB
/
Copy pathProgram_67.c
File metadata and controls
58 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Program 67: Write a program to sort an array using Merge Sort.
/*
Merge Sort is a divide-and-conquer sorting algorithm.
It works by dividing the array into two halves, recursively sorting each half, and then merging the two sorted halves to produce a single sorted array.
*/
#include <stdio.h>
// Function to merge two sorted sub-arrays
void merge(int arr[], int low, int mid, int high) {
int i = low, j = mid + 1, k = 0;
int temp[100];
while(i <= mid && j <= high) {
if(arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while(i <= mid)
temp[k++] = arr[i++];
while(j <= high)
temp[k++] = arr[j++];
for(i = low, k = 0; i <= high; i++, k++)
arr[i] = temp[k];
}
// Merge Sort function
void mergeSort(int arr[], int low, int high) {
if(low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
// Main function
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[100];
printf("Enter elements:\n");
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
mergeSort(arr, 0, n - 1);
printf("Sorted array:\n");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}