-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSortedArray.c
55 lines (52 loc) · 912 Bytes
/
MergeSortedArray.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
//C program to sort two arrays and merge them into single array
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr1[4]={1,2,3,4};
int arr2[6]={5,6,7,8,9,0};
int newarr[10];
int i=0,j=0,k=0;
while(i<4 && j<6)
{
if(arr1[i]<arr2[j])
{
newarr[k]=arr1[i];
i++;
k++;
}else
{
newarr[k]=arr2[j];
j++;
k++;
}
}
while(i<4)
{
newarr[k]=arr1[i];
i++;
k++;
}
while(j<6)
{
newarr[k]=arr2[j];
j++;
k++;
}
printf("Array 1:");
for(i=0;i<4;i++)
{
printf("%d",arr1[i]);
}
printf("\nArray 2:");
for(i=0;i<6;i++)
{
printf("%d",arr2[i]);
}
printf("\nSorted and Merged array :");
for(i=0;i<10;i++)
{
printf("%d",newarr[i]);
}
return 0;
}