The majority of merge sort implementations searched online are provided with unnecessary variables and code lines. Here is an attempt to reduce that. However, does passing back the subArray as return types around cause any extra overhead on space complexity due to the temporary auxiliary space requirements for the return values to keep on the recursive method call stack?
import static java.lang.System.out;
import java.util.Arrays;
/**
* @author thomas
*/
public class MergeSort {
public static void main(String[] args) {
int[] numArray = new int[]{4, 7, 2, 8, 1, 0, 2, 5, -4, 3};
out.println("Original array before sorting: " + Arrays.toString(numArray));
out.println("Ascending Sorted Array: " + Arrays.toString(mergeSort(numArray)));
out.println("Original array after sorting: " + Arrays.toString(numArray));
out.println("Descending Sorted Array: " + Arrays.toString(mergeSort(numArray, true)));
}
private static int[] mergeSort(int[] numArray) {
int[] subArray = Arrays.copyOfRange(numArray, 0, numArray.length);
return partition(subArray, false);
// return subArray; // return explicitly here with just calling the partition(subArray, false) and void return types for all the subsequent methods signature of partition() and onwards;
}
private static int[] mergeSort(int[] numArray, boolean isDescending) {
int[] subArray = Arrays.copyOfRange(numArray, 0, numArray.length);
return partition(subArray, isDescending);
// return subArray; // return explicitly here with just calling the partition(subArray, isDescending) and void return types for all the subsequent methods signature of partition() and onwards;
}
private static int[] partition(int[] subArray, boolean isDescending) {
if (subArray.length < 2) {
return subArray;
}
int mid = subArray.length / 2;
int[] leftArray = Arrays.copyOfRange(subArray, 0, mid);
int[] rightArray = Arrays.copyOfRange(subArray, mid, subArray.length);
partition(leftArray, isDescending);
partition(rightArray, isDescending);
return merge(subArray, leftArray, rightArray, isDescending);
}
private static int[] merge(int[] subArray, int[] leftArray, int[] rightArray, boolean isDescending) {
int leftIndex = leftArray.length, rightIndex = rightArray.length;
int leftArrayIncrementer = 0, rightArrayIncrementer = 0, mergedArrayIncrementer = 0;
while (leftArrayIncrementer < leftIndex && rightArrayIncrementer < rightIndex) {
if (leftArray[leftArrayIncrementer] <= rightArray[rightArrayIncrementer]) {
subArray[mergedArrayIncrementer++] = isDescending ? rightArray[rightArrayIncrementer++] : leftArray[leftArrayIncrementer++];
} else {
subArray[mergedArrayIncrementer++] = isDescending ? leftArray[leftArrayIncrementer++] : rightArray[rightArrayIncrementer++];
}
}
while (leftArrayIncrementer < leftIndex) {
subArray[mergedArrayIncrementer++] = leftArray[leftArrayIncrementer++];
}
while (rightArrayIncrementer < rightIndex) {
subArray[mergedArrayIncrementer++] = rightArray[rightArrayIncrementer++];
}
return subArray;
}
}
```
mergeSort
methods has the same signature (except the name of the method) as thepartition
method, so you should rename thepartition
tomergeSort
and get rid of the existingmergeSort
- the complete array copy is not really necessary I think. The othermergeSort
method is just a convenience thingy, but as it isprivate
, skip it and just specify the second parameter always. \$\endgroup\$