-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.c
81 lines (68 loc) · 1.41 KB
/
merge.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
71
72
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include<time.h>
void merge(int a[], int beg, int mid, int end){
int i,j,k;
int n1 =mid-beg+1;
int n2 = end-mid;
int leftarr[n1];
int rightarr[n2];
for(i=0;i<n1;i++){
leftarr[i] = a[beg+i];
}
for(j=0;j<n2;j++){
rightarr[j] = a[mid+1+j];
}
i=0; j=0;k=beg;
while(i<n1 && j<n2){
if(leftarr[i]<=rightarr[j]){
a[k] = leftarr[i];
i++;
}
else{
a[k] = rightarr[j];
j++;
}
k++;
}
while(i<n1){
a[k] = leftarr[i];
i++;
k++;
}
while(j<n2){
a[k] = rightarr[j];
j++;
k++;
}
}
void mergesort(int a[], int beg, int end){
if(beg<end){
int mid = (beg+end)/2;
mergesort(a, beg, mid);
mergesort(a, mid+1, end);
merge(a, beg, mid, end);
}
}
int main(){
int a[100000],i;
int j,num,temp;
clock_t st,et;
printf("Enter n: \n");
scanf("%d", &num);
for(i=0;i<num;i++){
a[i] = rand()%10000;
}
printf("Before sorting: \n");
for(i=0;i<num;i++){
printf("a[%d] = \t %d \n",i, a[i]);
}
st = clock();
mergesort(a, 0, num-1);
et = clock();
printf("After sorting: \n");
for(i=0;i<num;i++){
printf("a[%d] = \t %d \n",i, a[i]);
}
printf("Time taken: %f\n", (double)(et-st)/CLOCKS_PER_SEC);
return 0;
}