Question: Who can help me finish writing this algorithm for mergeSort? There comment guidelines in the code. /** Performs a merge sort using a given input
Who can help me finish writing this algorithm for mergeSort? There comment guidelines in the code.
/** Performs a merge sort using a given input array @param array the (unsorted) array @return the sorted array */ public E[] sort(E[] array) { if (array.length <= 1) return array; E[] sorted = array.clone(); E[] array2 = sorted.clone(); sort(sorted, array2, 0, array.length - 1); return sorted; }
private void sort(E[] array1, E[] array2, int first, int last) { if (first >= last) return;
int middle = (first + last) / 2; sort(array1, array2, first, middle); sort(array1, array2, middle+1, last);
int i = first; int a = first; int b = middle+1; while (a <= middle && b <= last) { // EXERCISE
// Copy the smaller of array[a] or array[b] to array2[i] // (in the case of a tie, copy array[a]) // and increment i and a or b (the one you copied). break; // DELETE
}
// EXERCISE // Copy the rest of a or b, whichever is not at the end.
System.arraycopy(array2, first, array1, first, last - first + 1); }
Step by Step Solution
There are 3 Steps involved in it
Implementing the merge sort algorithm Performs a merge sort using a ... View full answer
Get step-by-step solutions from verified subject matter experts
