-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.c
64 lines (59 loc) · 1.03 KB
/
merge_sort.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
#include <stdio.h>
void merge(int *a,int b,int mid,int e)
{
int ptr1=b,ptr2=mid+1,temp[6],i=0;
while(ptr1<=mid||ptr2<=e)
{
if(ptr1<=mid&&ptr2<=e)
{
if(a[ptr1]<=a[ptr2])
{
temp[i]=a[ptr1];
i++;ptr1++;
}
else if(a[ptr1]>=a[ptr2])
{
temp[i]=a[ptr2];
ptr2++;i++;
}
}
else if(ptr1>mid&&ptr2<=e)
{
temp[i]=a[ptr2];
ptr2++;i++;
}
else if(ptr1<=mid&&ptr2>e)
{
temp[i]=a[ptr1];
ptr1++;i++;
}
}
int j;
for(j=b;j<=e;j++)
{
a[j]=temp[j-b];
}
}
void sort(int *a,int b,int e)
{
if(b==e)
return;
else
{
int mid=(b+e)/2;
sort(a,mid+1,e);
sort(a,b,mid);
merge(a,b,mid,e);
}
}
int main()
{
int arr[6]={1,3,3,8,5,1};
sort(arr,0,5);
int i;
for(i=0;i<6;i++)
{
printf("%d ",arr[i]);
}
return 0;
}