Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 2: Mergesort 6 points; 2 points for each part,; individual-only Put all

ID: 3735197 • Letter: P

Question

Problem 2: Mergesort 6 points; 2 points for each part,; individual-only Put all of your work for this problem in a plain-text file named ps5pr2.txt. Given the following array: 124, 3, 27, 13, 34, 2, 50, 12 1. If the array were sorted using the version of mergesort presented in lecture, what would the array look like after the completion of the second call to the merge method-the method that merges two subarrays? Note: The merge method is the separate helper method; is not the recursive mSort method. 2. What would the array look like after the completion of the fourth call to the mergeO method? 3. The initial call to the recursive mSortO method is made from within the mergeSortO "wrapper" method. This initial call to mSortO is not a recursive call, because the method is not calling itself. Rather, all recursive calls to mSort are made from within mSortO. Assuming that the array has at least two elements, the initial invocation of mSortO makes two recursive calls (which in turn lead to other recursive calls, and so on). Starting with the initial array above, what would the array look like after the completion of the first of these two recursive calls? Important There will be no partial credit on the above questions, so please check your answers carefully!

Explanation / Answer

import java.util.*;

import java.io.*;

public class MergeSort {

public static void mSort(int[] arr, int[] temp, int start, int end) {

if (start < end) {

int middle = (start + end) / 2;

mSort(arr, temp, start, middle);

mSort(arr, temp, middle + 1, end);

merge(arr, temp, start, middle, middle + 1, end);

}

}

public static void mergeSort(int[] arr) {

int[] temp = new int[arr.length];

mSort(arr, temp, 0, arr.length - 1);

printArray(temp);

}

public static void merge(int arr[], int temp[], int start, int middle1, int middle2, int end) {

int i, j, k;

int n1 = middle2 - start;

int n2 = end - middle1;

int L[] = new int[n1];

int R[] = new int[n2];

for (i = 0; i < n1; i++){

temp[i] = arr[start+i];

L[i] = arr[start + i];

}

for (j = 0; j < n2; j++){

temp[j] = arr[middle2+j];

R[j] = arr[middle2 + j];

}

i = 0;

j = 0;

k = start;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

temp[k] = L[i];

i++;

} else {

temp[k] = R[j];

arr[k] = R[j];

j++;

}

k++;

}

while (i < n1) {

arr[k] = L[i];

temp[k] = L[i];

i++;

k++;

}

while (j < n2) {

arr[k] = R[j];

temp[k] = R[j];

j++;

k++;

}

}

public static void printArray(int[] arr) {

System.out.print("{ ");

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

System.out.println("}");

}

public static void main(String args[]) {

ArrayList<Integer> list = new ArrayList<Integer>();

try{

Scanner in = new Scanner(new File("ps5pr2.txt"));

int val;

while(in.hasNext()){

val = in.nextInt();

list.add(val);

}

int[] arr = new int[list.size()-1];

for(int i=0; i<list.size()-1; i++){

arr[i] = list.get(i);

}

mergeSort(arr);

}

catch(FileNotFoundException e){

System.out.println("Couldn't open");

System.exit(1);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote