-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge_Sort
72 lines (67 loc) · 1.25 KB
/
Merge_Sort
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
66
67
68
69
70
71
72
#include <stdio.h>
int main()
{
int n1;
printf("Enter the number\n");
scanf("%d",&n1);
int arr1[n1];
printf("Enter the first array element\n");
for(int i=0;i<n1;i++)
{
scanf("%d",&arr1[i]);
}
int n2;
printf("Enter the number for array two\n");
scanf("%d",&n2);
int arr2[n2];
printf("Enter the second array element\n");
for(int i=0;i<n2;i++)
{
scanf("%d",&arr2[i]);
}
int p1,p2,p3 = 0;
int n3 = n1+n2;
int arr3[n3];
while(p1<n1 && p2<n2)
{
if(arr1[p1]<arr2[p2])
{
arr3[p3] = arr1[p1];
p1++;
p3++;
}
else if(arr2[p2]<arr1[p1])
{
arr3[p3] = arr2[p2];
p2++;
p3++;
}
else
{
arr3[p3] = arr1[p1];
p3++;
arr3[p3] = arr2[p2];
p3++;
p2++;
p1++;
}
}
while(p1<n1)
{
arr3[p3] = arr1[p1];
p3++;
p1++;
}
while(p2<n2)
{
arr3[p3] = arr2[p2];
p3++;
p2++;
}
printf("After the merge sort array is:\n");
for(int i=0;i<n3;i++)
{
printf("%d\t",arr3[i]);
}
return 0;
}