Ask Question
8 July, 00:51

Problem 2 Write a method that (a) returns an int[], (b) takes in two sorted arrays (in descending order) and (c) returns an int [] with sorted values (in descending order) from the two sorted arrays. The method will also print out the number of compares done. int[] a = new int[]{18, 16, 15, 6, 2}; int[] b = new int[]{13, 12, 11, 7, 1}; public int[] sortArraysDes (int[] a, int[] b) { / /your code here } The int[] returned should have all the integers from int[]a and int[]b in descending order.

+2
Answers (1)
  1. 8 July, 02:31
    0
    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
    {

    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
    {

    System. out. print (ar[x] + " ");

    }

    }

    }

    Output:

    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.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Problem 2 Write a method that (a) returns an int[], (b) takes in two sorted arrays (in descending order) and (c) returns an int [] with ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers