(Solved) : Import Javautilarrays Public Class Mergesort Extends Consoleprogram Make Changes Method Pu Q43926618 . . .


![// Split the array in half int[) firstHalf = new int[arr.length / 21: int[] secondHalf - new int[arr.length - firstHalf.lengt](https://media.cheggcdn.com/media/725/725e7815-b89b-414b-aa79-84968ac0aaa1/image.png)

import java.util.Arrays;
public class MergeSort extends ConsoleProgram
{
/*
* Do not make changes to this method!
*/
public void run()
{
int[] array1 = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] array2 = {5, 6, 4, 8, 9, 7, 3, 1, 2};
System.out.print(“First array: “);
System.out.println(Arrays.toString(array1));
System.out.print(“Second array: “);
System.out.println(Arrays.toString(array2));
System.out.println();
// sort first array
mergeSort(array1);
// sort second array
mergeSort(array2);
System.out.print(“First array sorted: “);
System.out.println(Arrays.toString(array1));
System.out.print(“Second array sorted: “);
System.out.println(Arrays.toString(array2));
}
/*
* Merge sort takes in an array and sorts it.
*/
public static void mergeSort(int[] arr)
{
if (arr.length <= 1)
{
return;
}
// Split the array in half
int[] firstHalf = new int[arr.length / 2];
int[] secondHalf = new int[arr.length – firstHalf.length];
System.arraycopy(arr, 0, firstHalf, 0, firstHalf.length);
System.arraycopy(arr, firstHalf.length, secondHalf, 0,secondHalf.length);
// Sort each half
mergeSort(firstHalf);
mergeSort(secondHalf);
// Merge the halves together
merge(firstHalf, secondHalf, arr);
}
/*
* merge takes in three arrays. The first two are the two halves ofan array
* to be merged. The result is the resulting array that consists ofthe elements
* in the two half arrays, sorted.
*/
private static void merge(int[] firstHalf, int[] secondHalf, int []result)
{
// set up indices for iteration through arrays
int firstIndex = 0;
int secondIndex = 0;
int resultIndex = 0;
// while there are still elements in both halves, find which issmaller
// and add it to the result array first. Then, add thelarger.
while (firstIndex < firstHalf.length && secondIndex <secondHalf.length)
{
if (firstHalf[firstIndex] < secondHalf[secondIndex])
{
result[resultIndex] = firstHalf[firstIndex];
firstIndex++;
}
else
{
result[resultIndex] = secondHalf[secondIndex];
secondIndex++;
}
resultIndex++;
}
// There might be left over elements in one of the halves.
// Copy it over as well.
System.arraycopy(firstHalf, firstIndex, result, resultIndex,firstHalf.length – firstIndex);
System.arraycopy(secondHalf, secondIndex, result, resultIndex,secondHalf.length – secondIndex);
}
}

RUN CODE TEST CASES ASSIGNMENT DOCS GRADE MORE 10 points Status: Not Submitted Mergesort is a complicated algorithm, but how complicated is it? In this exercise, we’ll be taking our example code from before and adding a cool feature: at every recursive step, print out to the console what the two halves are that are going to be merged together. O 8.7.4: Explore Merge Sort 1 import java.util.Arrays; 2 public class MergeSort extends ConsoleProgram 4- 5 /* * Do not make changes to this method! */ public void run() 9- int[] array1 = (9, 8, 7, 6, 5, 4, 3, 2, 1}; int[] array2 = (5, 6, 4, 8, 9, 7, 3, 1, 2}; 10 %3D 11 12 System.out.print(“First array: “); System.out.printin(Arrays.toString(array1)); System.out.print(“Second array: “); System.out.println(Arrays.toString(array2)); System.out.println(); 13 14 15 16 17 18 // sort first array mergeSort(array1); // sort second array mergeSort(array2); 19 20 21 22 23 System.out.print(“First array sorted: “); System.out.println(Arrays.toString(array1)); System.out.print(“Second array sorted: “); System.out.printin(Arrays.tostring(array2)); 24 25 26 27 28 29 30 * Merge sort takes in an array and sorts it. */ public static void mergeSort(int[] arr) 31 32 33 34- if (arr,length <= 1) 35 36 returns 37 38 39 // Split the array in half int(1 firstHalf = new int[arr.length/ 2]: 40 41 gorithms and Recunlon // Split the array in half int[) firstHalf = new int[arr.length / 21: int[] secondHalf – new int[arr.length – firstHalf.length]; System.arraycopy(arr, 0, firstHalf, e, firstHalf.length); System.arraycopy(arr, firstHalf.length, secondHalf, e, secondHalf.length); 40 41 43 44 // Sort each half mergeSort(firstHalf); mergeSort(secondHalf); 46 47 // Merge the halves together merge(firstlalf, secondHalf, arr); 54- * merge takes in three arrays. The first two are the two halves of an array * to be merged. The result is the resulting array that consists of the elements * in the two half arrays, sorted. private static void merge(int[] firstHalf, int[] secondHalf, int [] result) 60- 61 I/ set up indices for iteration through arrays int firstIndex = 0; int secondIndex = 0; int resultIndex = 0; 63 64 66 // while there are still elements in both halves, find which is smaller I/ and add it to the result array first. Then, add the larger. swhile (firstIndex < firstHalf.length && secondIndex < secondHalf.length) 67 70- if (firstHalf[firstIndex] < secondHalf[secondIndex]) 71 result[resultIndex] – firstHalf[firstIndex); firstIndex++; 72- 73 74 else 76 77- secondHalf[secondIndex): result[resultIndex] = secondIndex++; rithms and Recurslon I/ set up indices for iteration through arrays int firstIndex- 0; int secondIndex = 0; int resultIndex = 0; 62 63 64 65 66 I/ while there are still elements in both halves, find which is smaller // and add it to the result array first. Then, add the larger. while (firstIndex < firstHalf.length && secondIndex < secondHalf.length) 67 68 69 70- if (firstlalf[firstIndex] < secondHalf(secondIndex]) 71 72- 73 result[resultIndex] – firstHalf[firstIndex]; firstIndex++; 74 75 else 76 77 result[resultIndex] – secondtalf[secondIndex]; secondindex++; 78 79 80 resultIndex++; 81 82 83 // There might be left over elements in one of the halves. // Copy it over as well. System.arraycopy(firstHalf, firstIndex, result, resultIndex, firstHalf.length – firstIndex); System.arraycopy(secondHalf, secondIndex, result, resultIndex, secondHalf.length – secondIndex); 84 85 86 87 88 89 lgorithms and Recurlon ConsoleProgram I1 ConsoleProgram allows you to read easily from the console. // Extend ConsoleProgram in order to use these helpful methods, readLine(String prompt); String name = readLine(“What is your name? “); readBoolean(String prompt); boolean likesIceCream – readBoolean(“Do you like ice cream? “); readDouble(String prompt); double gpa – readDouble(“What is your GPA? “); readInt(String prompt); readInt (“How old are you? “); int age = JAVA DE EERENCE Show transcribed image text RUN CODE TEST CASES ASSIGNMENT DOCS GRADE MORE 10 points Status: Not Submitted Mergesort is a complicated algorithm, but how complicated is it? In this exercise, we’ll be taking our example code from before and adding a cool feature: at every recursive step, print out to the console what the two halves are that are going to be merged together.
O 8.7.4: Explore Merge Sort 1 import java.util.Arrays; 2 public class MergeSort extends ConsoleProgram 4- 5 /* * Do not make changes to this method! */ public void run() 9- int[] array1 = (9, 8, 7, 6, 5, 4, 3, 2, 1}; int[] array2 = (5, 6, 4, 8, 9, 7, 3, 1, 2}; 10 %3D 11 12 System.out.print(“First array: “); System.out.printin(Arrays.toString(array1)); System.out.print(“Second array: “); System.out.println(Arrays.toString(array2)); System.out.println(); 13 14 15 16 17 18 // sort first array mergeSort(array1); // sort second array mergeSort(array2); 19 20 21 22 23 System.out.print(“First array sorted: “); System.out.println(Arrays.toString(array1)); System.out.print(“Second array sorted: “); System.out.printin(Arrays.tostring(array2)); 24 25 26 27 28 29 30 * Merge sort takes in an array and sorts it. */ public static void mergeSort(int[] arr) 31 32 33 34- if (arr,length
Expert Answer
Answer to import java.util.Arrays; public class MergeSort extends ConsoleProgram { /* * Do not make changes to this method! */ pub…