forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sorted_arr.c
79 lines (54 loc) · 852 Bytes
/
merge_sorted_arr.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdio.h>
int main()
{
int a[50],b[50],c[100];
int i,n1,n2;
printf("\nEnter number of elements in array 1: ");
scanf("%d", &n1);
printf("\nEnter your sorted array 1");
for(i=0;i<n1;i++)
{
printf("\nEnter %dst element: ", i+1);
scanf("%d", &a[i]);
}
printf("\nEnter number of elements in array 2: ");
scanf("%d", &n2);
printf("\nEnter your sorted array 2");
for(i=0;i<n2;i++)
{
printf("\nEnter %dst element: ", i+1);
scanf("%d", &b[i]);
}
int k=0;
int j=0;
i=0;
while(i<n1&&j<n2)
{
if(a[i]>b[j])
{
c[k++]=b[j++];
}
else if(a[i]<b[j])
{
c[k++]=a[i++];
}
else
{
c[k++]=a[i++];
c[k++]=b[j++];
}
}
while(i<n1)
{
c[k++]=a[i++];
}
while(j<n2)
{
c[k++]=a[j++];
}
printf("\nNew merged array\n");
for(i=0;i<(n1+n2);i++)
{
printf("%d", c[i]);
}
}