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
Dvinal [7]
3 years ago
7

Suppose you have two arrays of ints, arr1 and arr2, each containing ints that are sorted in ascending order. Write a static meth

od named merge that receives these two arrays as parameters and returns a reference to a new, sorted array of ints that is the result of merging the contents of the two arrays, arr1 and arr2. Note: you do not need to (and should not) sort here. Think of having two sorted piles of cards, that you're combining (merging) into another pile. You keep putting cards into the new pile, sometimes taking from one of your original piles, sometimes taking from the other. SUBMIT
Computers and Technology
1 answer:
telo118 [61]3 years ago
4 0
Since both arrays are already sorted, that means that the first int of one of the arrays will be smaller than all the ints that come after it in the same array. We also know that if the first int of arr1 is smaller than the first int of arr2, then by the same logic, the first int of arr1 is smaller than all the ints in arr2 since arr2 is also sorted.

public static int[] merge(int[] arr1, int[] arr2) {
int i = 0; //current index of arr1
int j = 0; //current index of arr2
int[] result = new int[arr1.length+arr2.length]
while(i < arr1.length && j < arr2.length) {
result[i+j] = Math.min(arr1[i], arr2[j]);
if(arr1[i] < arr2[j]) {
i++;
} else {
j++;
}
}
boolean isArr1 = i+1 < arr1.length;
for(int index = isArr1 ? i : j; index < isArr1 ? arr1.length : arr2.length; index++) {
result[i+j+index] = isArr1 ? arr1[index] : arr2[index]
}
return result;
}


So this implementation is kind of confusing, but it's the first way I thought to do it so I ran with it. There is probably an easier way, but that's the beauty of programming.

A quick explanation:

We first loop through the arrays comparing the first elements of each array, adding whichever is the smallest to the result array. Each time we do so, we increment the index value (i or j) for the array that had the smaller number. Now the next time we are comparing the NEXT element in that array to the PREVIOUS element of the other array. We do this until we reach the end of either arr1 or arr2 so that we don't get an out of bounds exception.

The second step in our method is to tack on the remaining integers to the resulting array. We need to do this because when we reach the end of one array, there will still be at least one more integer in the other array. The boolean isArr1 is telling us whether arr1 is the array with leftovers. If so, we loop through the remaining indices of arr1 and add them to the result. Otherwise, we do the same for arr2. All of this is done using ternary operations to determine which array to use, but if we wanted to we could split the code into two for loops using an if statement.


You might be interested in
Instructions: Use the tables below to answer the questions that follow:
Svet_ta [14]

Answer:

same as above....................

3 0
2 years ago
Compute the sum of all integers that are multiples of 9, from 1 to 250. Enter your result of your computation in the text box be
shepuryov [24]

The pseudocode to find the sum of all integers that are multiples of 9, from 1 to 250.

totalSum = 0

for i from 1 to 250{

     if i is divided by 9 and remainder is 0{

           totalSum  = totalSum + i;

     }

}

print(totalSum)


in python language the code will be

totalSum = 0

for i in range(1,250):

    if i%9==0:

       totalSum += i



If you will run the program , the answer would be 3402.

8 0
3 years ago
Read 2 more answers
Which of the following STEM discoverers is known as the “Prince of Math?”
Margarita [4]
The German mathematician & physicist ”Carl Friedrich Gauss”

Born: April 30, 1777, Brunswick, Germany
Died: February 23, 1855, Göttingen, Germany
8 0
3 years ago
Tools used to build a bridge<br>​
Sati [7]

Aerial Lifts,

Vertical Masts and Hydro Platforms,

Telehandlers,

Excavators,

Skid Loaders,

Backhoes,

Cranes,

Air Compressors.

If It was helpful, can you make me brainliest please?

6 0
2 years ago
Question / UJU
DIA [1.3K]

Answer:

Option D: If it is the month of January, then the temperature is cool.

Explanation:

A conditional statement is the one having an if condition in it that says, if the condition is true proceed to the next statement, else not.

Given statements are:

  • If it is the month of January, then it is winter.

This can be written as: if (January)⇒Winter

  • If it is winter, then the temperature is cool.

This can be written as: if (winter)⇒ Temperature(cool)

So by combining both the statements, the winter clause will be connected  and we get:

if(January)⇒Temperature (cool)

This can be written as:

  • If it is the month of January, then the temperature is cool.

So, Option D is the correct answer.

i hope it will help you!

5 0
3 years ago
Other questions:
  • During the past decade ocean levels have been rising faster than in the past, an average of approximately 3.1 millimeters per ye
    14·1 answer
  • A network administrator has been tasked with configuring access to the Internet for about 50 computers. The administrator has be
    6·1 answer
  • Which PowerPoint options can users customize?
    9·1 answer
  • All nuclear energy results in the rapid release of energy, such as in atomic bombs. true or false
    8·1 answer
  • How can you enter Task Manager in Windows? Select 3 options. press Ctrl + Shift + Tab press Ctrl+Alt+Delete and then click Task
    8·1 answer
  • How to control what is on the x and y axis in excel?
    13·1 answer
  • Claire writes a letter to her grandmother, in which she describes an amusement park she visited last week. She adds pictures of
    13·1 answer
  • What in the world is this for and how do you use it
    8·2 answers
  • What was original name<br> whoever answers first gets brainly crown
    9·1 answer
  • What is the difference between EPROM and EEPROM, explain why​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!