Respuesta :
Given that both arrays have already been sorted and that you wish to organize them in the conventional ascending order in this situation,
Create a third array, A3, with the size (A1 + A2), explore both arrays at once, and then check the element in the first array that is now selected.
Store it in the third array and increase the current if it is smaller than the second array's current element.
Store the seconds element in the third array and increase the seconds current if it is bigger than the current element in the second array.
The procedure still functions if it is equal to the element in the second; just proceed as before.
Add the remaining elements from the first array to the third array if there are any.
void merge(int A1[], size_t A1_sz,
int A2[], size_t A2_sz,
int A3[] )
{
int i; // A1 index
int j; // A2 index
int k; // A3 index
i = j = k = 0;
/* Traverse both arrays */
while ( (i<A1_sz) && (j<A2_sz) )
{
if ( A1[i] < A2[j] )
A3[k++] = A1[i++];
else
A3[k++] = A2[j++];
}
while (i < A1_sz) A3[k++] = A1[i++];
while (j < A2_sz) A3[k++] = A2[j++];
}
int main()
{
int A[4] = {1, 4, 8, 22};
size_t a_sz = 4;
int B[5] = {3, 6, 7, 11, 31};
size_t b_sz = 5;
int C[ a_sz + b_sz];
merge(A, a_sz, B, b_sz, C);
return 0;
}
merge run :
A[ (1) 4 8 22 ]
B[ (3) 6 7 11 31 ]
C[ ( ) ] Start
A[ 1 (4) 8 22 ]
B[(3) 6 7 11 31]
C[(1) ] 1 < 3
A[ 1 (4) 8 22 ]
B[ 3 (6) 7 11 31]
C[ 1 (3) ] 4 !< 3
A[ 1 (4) 8 22 ]
B[ 3 (6) 7 11 31]
C[ 1 3 4 ] 4 < 6
A[ 1 4 (8) 22 ]
B[ 3 (6) 7 11 31 ]
C[ 1 3 4 (6) ] 8 !< 6
A[ 1 4 (8) 22 ]
B[ 3 6 (7) 11 31 ]
C[ 1 3 4 (6) 7 ] 8 !< 7
A[ 1 4 (8) 22 ]
B[ 3 6 7 (11) 31 ]
C[ 1 3 4 6 7 8 ] 8 < 11
A[ 1 4 8 (22) ]
B[ 3 6 7 (11) 31]
C[ 1 3 4 6 7 8 11 ] 22 !< 11
A[ 1 4 8 (22) ]
B[ 3 (6) 7 11 (31)]
C[ 1 3 4 6 7 8 11 22 ] 22 < 31
i == 4 (skip) i !< a_sz (4)
j == 4
B[ 3 6 7 11 (31) ] j < b_sz (5)
C[ 1 3 4 6 7 8 11 22 31 ]
j == 5 j !< b_sz (5)
return
To know more about sorted arrays visit:-
https://brainly.com/question/14915529
#SPJ4