You are given two sorted arrays, A and B, and A has a large enough buffer at the
ID: 3587046 • Letter: Y
Question
You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by inputting 5 integers in the ascending order. (Note: don’t hard code the integers of the arrays.) (3) Merge B with A in a way all values are sorted. (4) Print out the updated array A, after merging with B. Write a method !!!
Explanation / Answer
This program will give answer as per condition of ascending order.Please go threw comment.
#include <stdio.h>
void sortFun(int a[],int b[])
{
int i=0,j=0,k=0;
i=0;
int count = 12; //0-12 if we will take 13 stack will give mess.
for(;i<5;i++) //Start with first element of b
{
for(;j<18;j++) //compare with first element of a and upto 18
{
if(b[i]<a[j]) // if b is less
{
for(k=count;k>=j;k--) //puss all element from that location to right side to make space for b
a[k+1]=a[k];
a[j]=b[i]; // finally add at that location
if(count < 18)
count++; // count will count number of element fill in a
break;
}
else if(j == count) // if condition will fail means b is greter so add at last
{
a[count]=b[i];
if(count < 18) // count shoul not more than 18
count++;
break;
}
}
}
for(i=0;i<18;i++)
printf("%d ",a[i]);
printf(" ");
}
void main(void)
{
int a[18],b[5];
int i=0;
printf("Please Enter value for A upto 13 ");
for(i=0;i<13;i++)
scanf("%d",&a[i]); //scan value for A from user
printf("Please Enter value for B upto 5 ");
for(i=0;i<5;i++)
scanf("%d",&b[i]); //scan value for B from user
sortFun(a,b);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.