given the function prototype void merge ( int a Given the function prototype: vo
ID: 3792470 • Letter: G
Question
given the function prototype void merge ( int a Given the function prototype: void merge(int a[], int b[], int c[], int na:int na, int nb) where na, and nb, represents of elements stored in arrays a and b assuming the contents of array a and b are already sorted in arrays a and b. are already sorted in ascending order and there are no duplicated values in them you are asked to write the code for the function to merge the contents of two arrays and have the merged results stored in array c whose size is large enough to contain all the merged values. Note that you must not use the sort algorithm or receive no points a sample diagram is provided below to help you visualize the situation 30%Explanation / Answer
void merge(int array[], int left, int z, int right)
{
int m, n, o;
int n1 = z - left + 1;
int n2 = right - z;
/* Temporary array is created here */
int Left[n1], Right[n2];
/* In temporary array Left[] and Right[] the data is copied */
for (m = 0; m < n1; m++)
Left[m] = array[l + m];
for (n = 0; n < n2; n++)
Right[n] = array[z + 1+ n];
/* Merger of the temp arrays back into array[l..r] is perforemd here*/
m = 0; // First subarray initial index
n = 0; // second subarray Initial index of
o = l; // merged subarray Initial index
while (m < n1 && n < n2)
{
if (Left[m] <= Right[n])
{
array[o] = Left[m];
m++;
}
else
{
array[o] = Right[n];
n++;
}
o++;
}
/* The remaining elements of Left will be copied, if any*/
while (m < n1)
{
array[o] = Left[m];
m++;
o++;
}
/* The remaining elements of Left will be copied, if any*/
while (n < n2)
{
array[o] = Right[n];
n++;
o++;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.