Answer:
Following are the Program in the Java Programming Language.
//define class
public class Main
{
//define function to merge and sort array
public static int[] sort_Array(int[] a, int[] b)
{
//set integer variable and initialize values
int x = 0, y = 0, k = 0, num = 0;
int n = a.length;
int n1 = b.length;
//set integer type array
int[] ar = new int[n+n1];
//set while loop
while (x<n && y <n1)
{
num++;
num++;
//set if conditional statement
if (a[x] > b[y])
ar[k++] = a[x++];
else
ar[k++] = b[y++];
num++;
}
//set the while loop
while (x < n)
{
num++;
ar[k++] = a[x++];
}
//set the while loop
while (y < n1)
{
num++;
ar[k++] = b[y++];
}
//print the total compares done
System.out.println("Number of compares done: " + n);
//return array
return ar;
}
//define main method
public static void main(String[] args)
{
//declaration and the initialization of array
int[] a = new int[]{18,16, 15, 6, 2};
int[] b = new int[]{13, 12, 11, 7, 1};
int[] ar;
//call function
ar = sort_Array(a, b);
//print message
System.out.println("\nThe resultant array is: \n");
//set for loop to print array
for(int x=0; x<ar.length; x++)
{
System.out.print(ar[x] + " ");
}
}
}
<u>Output</u>:
Number of compares done: 5
The resultant array is:
18 16 15 13 12 11 7 6 2 1
Explanation:
Here, we define a class "Main" inside the class, define a function "sort_Array()"and pass two integer type array argument "a" and "b".
- Set four integer type variables "x", "y", "k", and "num" and initialize them to 0.
- Set two integer type array "n", "n1" and initialize them the length of the array then, set integer type array variable "ar".
- Set the while loop then, set if conditional statement then, agin set while loop to sort array in descending order.
- Then, print the number of compares and return the value of the variable "ar".
Finally, define the main method inside it, we set two integer type array variable and initialize elements in them then, set the for loop to print the sorted elements of the array.