In: Computer Science
The program is given below: that define two array A and B, initialize array by taking integers in the ascending order from user, then call merge() method that merge B with A in a way all values are sorted, print updated array A, after merging with B.
#include <stdio.h>
//merge() method merge B with A in a way all values are
sorted
void merge(int A[],int B[])
{
int i=13-1,j=5-1;
int k=17;
//continue until j greater than equal 0
while(j>=0)
{ //if i is greater than equal 0 and A[i] is greater than
B[j]
if(i>=0 && A[i]>B[j])
{ //set A[k] equal to A[i]
A[k]=A[i];
//decrement i by 1
i--;
}
else
{ //set A[k] equal to B[j]
A[k]=B[j];
//decrement j by 1
j--;
}
//decrement k by 1
k--;
}
}
int main()
{
//define two array A and B
int A[18],B[5],i;
//execute for loop from 0 to 13
for(i=0;i<13;i++)
{ //take integers in the ascending order from user
scanf("%d",&A[i]);
}
//execute for loop from 0 to 5
for(i=0;i<5;i++)
{ //take integers in the ascending order from user
scanf("%d",&B[i]);
}
//call merge() method that merge B with A in a way all values are
sorted
merge(A,B);
//Print updated array A, after merging with B
printf("\n");
for(i=0;i<18;i++)
{
printf("%d ",A[i]);
}
return 0;
}
The screenshot of code is given below:
Input:
1 3 11 15 20 25 34 54 56 59 66 69 71
2 4 5 22 40
Output: