-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3b.c
64 lines (50 loc) · 912 Bytes
/
3b.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>
int arr[100];
int merge(int arr[],int l,int m,int h)
{
int arr1[10],arr2[10];
int n1,n2,i,j,k;
n1=m-l+1;
n2=h-m;
for(i=0;i<n1;i++)
arr1[i]=arr[l+i];
for(j=0;j<n2;j++)
arr2[j]=arr[m+j+1];
arr1[i]=9999;
arr2[j]=9999;
i=0;j=0;
for(k=l;k<=h;k++)
{
if(arr1[i]<=arr2[j])
arr[k]=arr1[i++];
else
arr[k]=arr2[j++];
}
return 0;
}
int merge_sort(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
merge(arr,low,mid,high);
}
return 0;
}
int main()
{
int n,i;
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
merge_sort(arr,0,n-1);
printf("Sorted array:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
return 0;
}