-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge_sort.c
More file actions
58 lines (58 loc) · 933 Bytes
/
merge_sort.c
File metadata and controls
58 lines (58 loc) · 933 Bytes
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
#include<conio.h>
#include<stdio.h>
#define size 8
void merge(int *,int,int);
void merges(int *,int,int);
void main()
{
int i,s[size],f;
char g;
printf("enter elements...");
for(i=0;i<size;i++)
scanf("%d",&s[i]);
printf("\nA-ascending\nD-descending\n");
get:g=getch();
if(g=='a'||g=='A')
f=1;
else if(g=='d'||g=='D')
f=-1;
else
goto get;
merge(&s[0],size,f);
printf("\nsorted elements...");
for(i=0;i<size;i++)
printf("%d ",s[i]);
}
void merge(int *a,int i,int f)
{
if(i==1)
return;
merge(a,i/2,f);
merge(a+i/2,(i+1)/2,f);
merges(a,i,f);
}
void merges(int *a,int i,int f)
{
int b[i/2],c[(i+1)/2],bi=i/2,ci=(i+1)/2,k,p=0,r=0;
for(k=0;k<bi;k++)
b[k]=*(a+k);
for(k=0;k<ci;k++)
c[k]=*(a+k+bi);
for(k=0;k<i&&p<bi&&r<ci;k++)
{
if(b[p]*f<c[r]*f)
{
*(a+k)=b[p];
p++;
}
else
{
*(a+k)=c[r];
r++;
}
}
for(;k<i&&p<bi;k++,p++)
*(a+k)=b[p];
for(;k<i&&r<ci;k++,r++)
*(a+k)=c[r];
}