-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMergeSort.c
64 lines (60 loc) · 936 Bytes
/
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
#include<stdio.h>
#define SIZE 20
void MERGESORT(int*,int,int);
void MERGE(int*,int,int,int);
main()
{
int A[SIZE],n,i;
printf("Enter n : ");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;++i)
scanf("%d",A+i);//end of loop
MERGESORT(A,0,n-1);
for(i=0;i<n;++i)
printf("%d ",A[i]);//end of loop
printf("\n");
}//end of main
void MERGESORT(int *A,int BEG,int END)
{
int MID;
if(BEG<END)
{
MID=(BEG+END)/2;
MERGESORT(A,BEG,MID);
MERGESORT(A,MID+1,END);
MERGE(A,BEG,END,MID);
}
}//end of function
void MERGE(int *A,int BEG,int END,int MID)
{
int I=BEG,J=MID+1,K=BEG,C[20];
while(I<=MID && J<=END)
{
if(A[I]<A[J])
{
C[K]=A[I];
++I;
}
else
{
C[K]=A[J];
++J;
}
++K;
}//end of loop
while(I<=MID)
{
C[K]=A[I];
++I;
++K;
}//end of loop
while(J<=END)
{
C[K]=A[J];
++J;
++K;
}//end of loop
for(I=BEG;I<K;++I)
A[I]=C[I];//end of loop
}//end of function