1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
SSSSS [86.1K]
3 years ago
13

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.
Computers and Technology
1 answer:
Galina-37 [17]3 years ago
4 0

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.

You might be interested in
Critics of media consumption
Mumz [18]

Answer:

b

Explanation:

make me brainlyest plzz

sorry if i got it wrong

5 0
3 years ago
Read 2 more answers
Which statement is unnecessary inside the unit test for the custom controller?
Anna [14]

Answer:

A. public ExtendedController(ApexPages.StandardController cntri) { }

Explanation:

A wizard is generally referred to as the tab sequence that contains the back button as well as the next button. It usually accepts information (i.e. data) from the inputs of users from different pages of the visualfoce as well as a variable from the previous website. The statement in option A is unnecessary for the application of the custom controller.    

5 0
4 years ago
A program uses two classes: dog and poodle. which class is the base class and which is the derived class?
Vsevolod [243]
Dog is the base class and poodle is the derived class. 
6 0
4 years ago
Read 2 more answers
How have computers changed overtime
sattari [20]
So in the beginning, a regular computer took up the space of an entire room it was literally that big in size. now and days computers are this small object that you can carry around with you in your bag.
4 0
3 years ago
Read 2 more answers
In spreadsheets, a cell reference is also referred to as which of the following? A. active cell B. axis C. cell address D. entry
nignag [31]
Generally its referring to the cells location, so C.
4 0
3 years ago
Other questions:
  • Write code that prints: Ready! firstNumber ... 2 1 Run!
    12·1 answer
  • 1. You want to schedule a weekly analysis for the Windows servers in your data center. The command should run as a scheduled job
    15·1 answer
  • What is a main cause of a virus on a computer
    6·1 answer
  • In python:
    8·1 answer
  • A(n) ____ is a location on your computer screen where you type text entries to communicate with the computer’s operating system.
    9·2 answers
  • If an additional term relates to payment, quality, quantity, price, time, and place of delivery, the CISG considers the added te
    12·1 answer
  • What are the main dimensions of information system and their components
    13·1 answer
  • The while loop and the do loop are equivalent in their expressive power; in other words, you can rewrite a while loop using a do
    6·1 answer
  • Why does my wifi keep disconnecting and reconnecting?
    9·1 answer
  • Legends are titles given to three-axis X, Y, and Z-axis. True or false?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!