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
Explain the application software and utility software in detail​
Vikentia [17]

Utility software is software designed to help analyze, configure, optimize or maintain a computer. It is used to support the computer infrastructure - in contrast to application software, which is aimed at directly performing tasks that benefit ordinary users.

In information technology, an application (app), application program or application software is a computer program designed to help people perform an activity. Depending on the activity for which it was designed, an application can manipulate text, numbers, audio, graphics, and a combination of these elements.

7 0
3 years ago
The linux/unix command __________ can be used to search for files or contents of files.
Nana76 [90]
The `grep` command is used for this. https://man7.org/linux/man-pages/man1/grep.1.html
8 0
2 years ago
To locate all the documents in a Mongo database that match an expression, use the _____________ method.
Lena [83]

Answer:

find()

Explanation:

When dealing with MongoDB the method that needs to be used in this scenario would be the find() method. This method basically returns all of the records that exist in the collection on which it is called, if no parameter is passed. If you pass a parameter/expression then only the records that match completely the expression will be returned to the user, otherwise nothing is returned. For example, on a database (db) we would call the following

db.find() ... This will return all records

db.find({specific}) ... This will return only the records that match specific.

4 0
3 years ago
How does Shakespeare immediately introduce Tybalt as a menacing character?
Scilla [17]

The correct answer to this open question is the following.

Although there are no options attached we can say the following.

William Shakespeare immediately introduces Tybalt as a menacing character in the first moments of the play "Romeo and Juliet" because Tybalt is an aggressive man who does not support the idea of peace or get peaceful agreements to solve problems. Tybalt does not like the Montague people.

“The tragedy of Romeo and Juliet” was written by the English writer William Shakespeare approximately in 1590 and is considered to be a play in a poetic form. It refers to the story of two lovers that belonged to opposite families in dispute. Their love is forbidden. Eventually, both lovers die and that is the reason for their families to reconcile.

5 0
3 years ago
4.2 code need help plz someone 15 points if u help
Korolek [52]

def func():  

 total = 0

 while True:

   pet = input("What pet do you have? ")

   if pet == "rock":

     return

   total += 1

   print("You have a {} with a total of {} pet(s)".format(pet, total))

func()

We wrapped our code in a function so that whenever the user enters rock, we can simply return and exit the function. If you have any other questions, I'll do my best to answer them.

8 0
3 years ago
Other questions:
  • Does anyone have any social media message me
    14·1 answer
  • What type of computer is likely to use so-dimms, have an internal power supply, and use a desktop processor socket?
    10·1 answer
  • When creating a mail merge, you must insert all of the items from your data source into your merged document. Truth or false ?
    6·2 answers
  • You have been approached by the network administrator of the company whose office is adjacent to yours. He has advised you to mo
    5·1 answer
  • A _____ is inserted so that a portion of a document that can have different formatting from the rest of the document. a. heading
    9·1 answer
  • Your company has 1,000 users in a Microsoft Office 365 subscription. A Power BI administrator named Admin1 creates 20 dashboards
    14·1 answer
  • A technician selected the GUI interface on a wireless router and added a DNS address of 208.67.220.220. The technician then imme
    6·1 answer
  • When troubleshooting firewalls, you should never attempt to repeat the problem because you could do more damage. T/F
    6·1 answer
  • (main.c File)
    7·1 answer
  • When a value of one data type is automatically changed to another data type, a(n) ____________________ type coercion is said to
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!