-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_mergesort.c
108 lines (52 loc) · 999 Bytes
/
5_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//Govind J Nair
//S3-D
//23
#include<stdio.h>
#define max 25
int main()
{
int arr[max],temp[max],i,j,k,n,size,x1,y1,x2,y2;
printf("enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the elements %d :",i+1);
scanf("%d",&arr[i]);
}
for(size=1; size < n; size=size*2 )
{
x1=0;
k=0;
while(x1+size<n)
{
y1=x1+size-1;
x2=y1+1;
y2=x2+size-1;
if(y2>=n)
y2=n-1;
i=x1;
j=x2;
while(i<=y1&&j<=y2)
{
if( arr[i]<=arr[j] )
temp[k++]=arr[i++];
else
temp[k++]=arr[j++];
}
while(i<=y1)
temp[k++]=arr[i++];
while(j<=y2)
temp[k++]=arr[j++];
x1=y2+1;
}
for(i=x1; k<n; i++)
temp[k++]=arr[i];
for(i=0;i<n;i++)
arr[i]=temp[i];
}
printf("Sorted list is :\n");
for( i = 0 ; i<n ; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}