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]
4 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]4 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
Select the correct text in the passage.
MatroZZZ [7]

Answer:

Answer:Veronica works as an executive in a marketing company.

Explanation:

Today, she has to make a presentation to her advertising agency about market research findings for a new product. Her manager will be assessing her presentation. Veronica has spent the whole week working on the presentation, and she wants to make a good impression. She has used the latest software to develop her presentation. She thought the presentation came out well and that it should give the advertising agency the information it needs to make a new commercial. As she enters the conference room, she sets up her laptop and plugs in the USB drive that has the presentation on it. However, she notices that the files on the USB drive have become corrupt. If she hadn't saved the presentation, she would have had to reschedule the meeting for next week. However, Veronica has a backup of the

8 0
3 years ago
What steps will change an existing macro? Use the drop-down menu to complete the steps.
Aneli [31]

Answer:

1. View

2. View macros

3. Edit

Explanation: completed on edge

6 0
3 years ago
Read 2 more answers
What is the meaning of antimonographycationalis​
Oliga [24]

Answer:

Although this word was made up in order to be a contender for the longest word in English, it can be broken down into smaller chunks in order to understand it.

Anti- means that you are against something; monopoly means the exclusive control over something; geographic is related to geography; and the remaining part has to do with nationalism.

So this word means something like 'a nationalistic feeling of being against geographic monopoly,' but you can see that it doesn't have much sense.

(answer copied from  Kalahira just to save time)

3 0
3 years ago
Read 2 more answers
Companies use virtualization to do all of the following except:
dangina [55]

Answer:

c)

Explanation:

Virtualization refers to running a virtual instance of a computer system apart from the actual software. Companies use this virtualization to do all of the following except Reduce the amount of applications in corporate datacenters. Instead few systems have the applications and those systems are cloned through virtualization.

4 0
3 years ago
A wet-carpet cleaner that carries its own water supply has water pressure created by a        A. faucet mixing valve.   B. manua
BARSIC [14]
Your answer is most likely C. motor-driven pump
5 0
3 years ago
Other questions:
  • ________ is the information about a file.
    7·1 answer
  • Pls go to my account and answer my question
    11·2 answers
  • In your Ice Breakers game, when does the player lose a life?
    6·2 answers
  • Write the definition of a function printLarger, which has two int parameters and returns nothing. The function prints the larger
    7·1 answer
  • The rules of right-of-way<br> Pedestrians, bicyclists, and skateboarders<br> when they use the road.
    12·1 answer
  • On tool hackers use to get sensitive information from victims is/are:
    15·2 answers
  • Which of these examples demonstrate portfolio income?
    13·2 answers
  • How do i know my child's login info for parent infinite campus
    14·1 answer
  • What describes the current cloud landscape for business?
    12·1 answer
  • What is the best example of how computers have changed the way people communicate?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!