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
What are color highlights?
SVETLANKA909090 [29]

A- overusing highlights lowers the contrast and degrads the effect of the highlights.

3 0
3 years ago
Read 2 more answers
Have you seen this ProFlipperz Review? It is a course on how to flip every day household items for a profit.
Nadusha1986 [10]

Well if you have one I know it's cool but don't flip it all the time

Explanation:

6 0
2 years ago
Spoken word and written text are different because?
Dahasolnce [82]
There are several differences between spoken words and written texts, among these differences are the following:
1- Spoken words have tones that show the feelings of the speaker, whereas, the written text is expressionless.
2- Spoken works might have inaccurate grammar or sentence structure, whereas, the written text has correct syntax and perfect grammar
3- Written text has a sort of a standard form that can be understood by anyone who speaks the language that the text is written in, regardless the dialect or the accent.
6 0
3 years ago
Read 2 more answers
Which value for the display property is useful when configuring horizontal navigation within an unordered list?
ikadub [295]
The Block:Inline is the value for the display property which is very useful when configuring horizontal navigation within an unordered list.The element will be still supplied as an inline element even in actual it is really a block element.
6 0
3 years ago
MaKe TeA In ThE AnSwErs
AfilCa [17]
Pineapples don’t taste good.
4 0
3 years ago
Other questions:
  • Which of the following activities poses the greatest personal cybersecurity risk? A. Making a purchase on an online store that u
    13·1 answer
  • Which increases the rate of soil formation?
    10·1 answer
  • ServletConfig defines a set of methods that a servlet uses tocommunicate with its servlet container.
    5·1 answer
  • What is the description of a computer ram?
    7·1 answer
  • -The following type of Access form allows to see multiple records in an Excel-like arrangement of data fields:
    14·1 answer
  • Which of the following is a command shell with a built-in scripting language?1. The Server Manager’s Roles and Features wizard2.
    11·2 answers
  • What will the following segment of code output?
    7·1 answer
  • A friend was just promoted to a new job that requires part-time travel, and he has also been promised a new laptop after his fir
    10·1 answer
  • To add a new kind of information into the database you need to add a new table<br> true or false?
    7·2 answers
  • if a user has one column in a table and would like to make it into two columns which command should be used
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!