-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.c
70 lines (64 loc) · 1.25 KB
/
MergeSort.c
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
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
void display(int *, int );
void mergeSort(int *array, int n);
void merge(int *a, int m,int *b, int n, int *array);
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int *array = (int *)malloc(n * sizeof(int));
int *temp = (int *)malloc(n * sizeof(int));
for(int i = 0; i < n; i++) {
scanf("%d",&array[i]);
}
display(array,n);
mergeSort(array,n);
display(array,n);
return 0;
}
void display(int *a, int n) {
for(int i = 0; i < n; i++) {
printf("%d ",a[i]);
}
printf("\n");
}
void mergeSort(int *array, int n) {
int middle = n / 2;
int *a = (int *)malloc(middle * sizeof(int));
int *b = (int *)malloc((n - middle) * sizeof(int));
if(n > 1) {
for(int i = 0; i < middle; i++){
a[i] = array[i];
}
for(int i = middle; i < n; i++) {
b[i - middle] = array[i];
}
mergeSort(a,middle);
mergeSort(b,n-middle);
merge(a,middle,b,n-middle,array);
free(a);
free(b);
}
}
void merge(int *a, int m,int *b, int n, int *array) {
int i = 0, j = 0, k = 0;
while(i < m && j < n) {
if(a[i] <= b[j]) {
array[k] = a[i];
i++;
}else {
array[k] = b[j];
j++;
}
k++;
}
while(i < m){
array[k] = a[i];
i++;k++;
}
while(j < n){
array[k] = b[j];
j++;k++;
}
}