Question
Above the java code below, write a main method to merge sort an array of numbers. Write the merge part to do this. public static
Above the java code below, write a main method to merge sort an array of numbers. Write the merge part to do this.
public static mergeSort(a, tempArray, first, last); } // end mergeSort private static void mergeSort(T[] a, T[] tempArray, int first, int last) { if (first < last) { // sort each half int mid = first + (last - first) / 2; // Index of midpoint mergeSort(a, first, mid); // Sort left half array[first..mid] mergeSort(a, mid + 1, last); // Sort right half array[mid+1..last] if (a[mid].compareTo(a[mid + 1]) > 0) // Question 2, Chapter 9 merge(a, tempArray, first, mid, last); // merge the two halves // else skip merge step } // end if } // end mergeSort private static void merge(T[] a, T[] tempArray, int first, int mid, int last) { // Two adjacent subarrays are a[beginHalf1..endHalf1] and a[beginHalf2..endHalf2]. int beginHalf1 = first; int endHalf1 = mid; int beginHalf2 = mid + 1; int endHalf2 = last; // While both subarrays are not empty, copy the // smaller item into the temporary array int index = beginHalf1; // Next available location in tempArray }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started