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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!