-
Notifications
You must be signed in to change notification settings - Fork 1
/
mergesort.c
65 lines (59 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
65
#include<stdio.h>
#include<stdlib.h>
void merge(int A[], int l, int mid, int h){
int B[100],i,j,k;
i = l;
j = mid+1;
k = i;
while(i<=mid && j<=h)
{
if(A[i]<A[j])
{
B[k++] = A[i++];
}
else{
B[k++] = A[j++];
}
}
for(;i<=mid;i++)
{
B[k++] = A[i];
}
for(;j<=h;j++)
{
B[k++] = A[j];
}
for(int i=l;i<=h;i++)
{
A[i] = B[i];
}
}
void IMergeSort(int A[], int n)
{
int p,l,h,mid,i;
for(p=2;p<=n;p=p*2)
{
for(i=0;i+p-1<n;i=i+p)
{
l=i;
h=i+p-1;
mid=(l+h)/2;
merge(A,l,mid,h);
}
}
if(p/2<n)
{
merge(A,0,p/2-1,n-1);
}
}
int main()
{
int A[10] = {1,5,2,1,6,3,6,2,5,10};
int i;
IMergeSort(A,10);
for(i=0;i<10;i++)
{
printf("%d ",A[i]);
}
return 0;
}