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
Favorite color should it be stored why?or why not?<br>​
Triss [41]

Answer:

Explanation:

green

8 0
2 years ago
The term _____ best describes the level of technology skills needed in today’s business world.A: computer knowledgeB: computer f
Alja [10]

Answer:

The answer is "Option B"

Explanation:

The ability to learn and use basic computer hardware and software understanding to solve problems and to access information is known as computer fluency. and other options are not correct, that can be defined as follows:

  • In option A, computer knowledge is a basic general awareness, that helps the user to use the computer more fluently, that's why it is not correct.
  • In option C, It is an internal feature of the computer, that helps to transfer data, that's why it is not correct.
  • In option D, computer information is also known as data that is stored in the computer, that's why it is not correct.
6 0
3 years ago
Im lonellly whos down to date me
inna [77]

Answer:

I was actually just looking to help with someone's schoolwork. . .

Explanation:

5 0
2 years ago
Read 2 more answers
What is the QOS model?
Andre45 [30]

Answer:

Quality Of Service

Explanation:

Technology that manages data traffic to reduce packet loss, latency and jitter on the network. QoS controls and manages network resources by setting priorities for specific types of data on the network.

5 0
2 years ago
What is data mining ​
Vesna [10]

Answer:

Data mining is a process of extracting and discovering patterns in large data sets involving methods at the intersection of machine learning, statistics, and database systems.

5 0
3 years ago
Read 2 more answers
Other questions:
  • Whenever you are passing or another vehicle is passing you, _______ is particularly important to avoid collisions. A. Turning. B
    9·2 answers
  • Microsoft ________ is a cloud storage and file sharing service
    12·1 answer
  • Let’s define a new language called dog-ish. A word is in the lan- guage dog-ish if the word contains the letters ’d’, ’o’, ’g’ a
    6·1 answer
  • Write a function all_cats that consumes a comma-separated string of animals and prints whether all of the animals have "cat" in
    12·1 answer
  • The first step in the information processing cycle is _____, which involves entering data into the computer.
    8·1 answer
  • Since the web.xml file describes how the web application should be configured when it is deployed on a server, the file is known
    7·1 answer
  • What will be result of below if statement.
    13·1 answer
  • When machining rotors, what is the reason for setting the indexing collars to zero?
    9·1 answer
  • How can you say that a painting is real? ​
    7·2 answers
  • Please i relly need this! ​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!