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
A typical, small, dry cell battery has a voltage of A. 6.0 volts. B. 1.0 volts. C. 12.0 volts. D. 1.5 volts.
Bad White [126]
The answer is D 1.5 volts
6 0
3 years ago
Read 2 more answers
John works in the Sales and Marketing group. Why does his group need to interact with operations management to plan the launch o
fredd [130]

Answer:

Operations management will put everything in place to make and distribute the new product.

Explanation:

The sales and marketing group could make a great promotion campaign but if the product isn't ready or isn't in the stores shelves by the moment the campaign hits the media it will be useless.

The operations management's job to put everything in place to make and distribute the new product.

Other choices are not right because....

... will market the new product: no, it's the Sales and Marketing's job to do it.

... hire new employees: no, it's he human ressources' job to do it.

... will sell the new product: no, it's the Sales and Marketing's job to do it.

4 0
3 years ago
which filename refers to a 16-bit real-mode program that queries the system for device and configuration data, and then passes i
Viefleur [7K]

The filename that refers to a 16-bit real-mode program that queries the system for device and configuration data, and then passes its findings to ntldr is NTDetect.com.

<h3>What does a Ntdetect file do?</h3>

A part of Microsoft Windows NT-based operating systems that utilize the x86 architecture is ntdetect.com. It is utilized throughout the Windows NT startup process and is in charge of determining what fundamental hardware will be needed to launch the operating system.

Your computer starts the Windows XP operating system using the NTLDR and Ntdetect.com files, which are crucial system files. Occasionally, these files may get damaged, corrupted, or lost.

Learn more about program from

brainly.com/question/26497128
#SPJ1

5 0
1 year ago
There is overlap in the subjects of study in the different information technology disciplines. true or false
OlgaM077 [116]

The question statement is true that there is overlap in the subject of study in different information technology discipline.

It is true that there is overlap in the subjects of study in the different information technology disciplines. The disciplines are collectively known as the disciplines of computing. Computer science, computer engineering, information technology, information systems, and software engineering all come in the realm of computing disciplines. These are the five distinct computing disciplines with overlapping subjects of study.

These disciplines are said to be interrelated as computing is their main area of study, yet they are distinct since each has its own curricular focus, research perspective and career prospects.

  • Computer science: Computer science as the discipline deals with designing and implementation of operating systems and software, including the study of computation and computability.
  • Computer engineering: As a discipline, computer engineering concerns with making different parts of computers work together. Research, design and development of computer-based equipment and hardware are also area of study in this discipline.
  • Information technology: Information technology as an area of study is mainly concerned with automation of business operations. It provides support through software and computers to handle data and innovate business processes.
  • Information systems: Information system as a discipline provides area of study which makes people able to apply advance information technology to solve today’s problem primarily within an organizational and enterprise settings.
  • Software engineering:  Software engineering is the discipline concerns with the designing, development, maintenance, testing, and evaluation of computer software and applications to solve real world problems.

You can learn more about information technology at

brainly.com/question/14688347

#SPJ4

7 0
1 year ago
To apply format to text, both the text and the text box must be selected.
Xelga [282]
I say it is true
hope this helps!
5 0
3 years ago
Other questions:
  • Solve system of equations. <br>x+2y-z=4<br>2x-y+3z=8 <br>-2x+3y-2z=10
    12·1 answer
  • Exit network systems, information support, and software development are all careers in which career cluster?
    10·1 answer
  • In 125 words describe the steps to active listening.
    6·1 answer
  • All users on the network have antivirus software; however, several users report that they have what an administrator described a
    10·1 answer
  • Renee is creating a multimedia presentation for a website that requires user interaction. Which multimedia type is Renee using?
    5·2 answers
  • Which of the following is a programming language that permits Web site designers to run applications on the user's computer?
    15·1 answer
  • Anyone down to play gta later i play on ps4?
    15·2 answers
  • An application's certificate indicates the application -
    5·1 answer
  • 4. Compute the following additions
    7·1 answer
  • Microsoft recommends when that you create passwords with upper and lowercase letters, numbers, symbols, and use at least _______
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!