I have written a MergeSort implementation that should sort an array of any data type.
Am facing 2 issues here.
Since Generic types cannot be defined as an array as the compiler errors out saying that 'cannot create a generic array of T', I have followed a kludge suggested in one of the threads in StackOverflow to use
T[] right = (T[]) Array.newInstance(clazz, rightSize);
so that with the type info supplied during runtime, we still can instantiate an array and cast it back to T[] though it is still vulnerable to ClassCastException irrespective of whether we handle it or not.
Is there any other efficient and ideal way of achieving this?
- Now the below merge sort algorithm works based on Generics, I cannot supply an array of primitive types like int[] in the place of T[].
So should i need to create the MergeSort class one version for each of primitive typed array that i wanted to sort?
- In general, can this algorithm be improved further?
Any help would be greatly appreciated. Thanks.
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
public class MergeSort {
public <T> void sort(T[] values, Class<T> clazz, Comparator<T> comparator) {
if (values == null) {
throw new IllegalArgumentException("values is null.");
}
// recursion exit criteria.
if (values.length < 2) {
return;
}
// segregate the values array into 2 halves.
int median = values.length / 2;
int leftSize = median;
int rightSize = values.length - median;
// construct the left array.
T[] left = (T[]) Array.newInstance(clazz, leftSize);
for (int l = 0; l < leftSize; ++l) {
left[l] = values[l];
}
// construct the right array.
T[] right = (T[]) Array.newInstance(clazz, rightSize);
for (int r = 0; r < rightSize; ++r) {
right[r] = values[leftSize + r];
}
// recursively do merge sort on either side of the array.
sort(left, clazz, comparator);
sort(right, clazz, comparator);
// merges the left and right and keeps the intermediate
// values array sorted as it works it's way up.
_merge(values, left, right, comparator);
}
private <T> void _merge(T[] values, T[] left, T[] right, Comparator<T> comparator) {
int leftIndex = 0;
int rightIndex = 0;
int sortedIndex = 0;
while (leftIndex < left.length && rightIndex < right.length) {
int comparison = comparator.compare(left[leftIndex], right[rightIndex]);
if (comparison <= 0) {
values[sortedIndex] = left[leftIndex];
leftIndex++;
} else {
values[sortedIndex] = right[rightIndex];
rightIndex++;
}
sortedIndex++;
}
// Handle the left over elements if any in the left side
// and places them in the sorted array.
while (leftIndex < left.length) {
values[sortedIndex] = left[leftIndex];
leftIndex++;
sortedIndex++;
}
// Handle the left over elements if any in the right side.
// and places them in the sorted array.
while (rightIndex < right.length) {
values[sortedIndex] = right[rightIndex];
rightIndex++;
sortedIndex++;
}
}
public static void main(String[] args) {
Integer[] values = new Integer[] { 5, 0, 10, 4, 1, 8, 3, 9, 6, 2, 7 };
new MergeSort().sort(values, Integer.class, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println(Arrays.toString(values));
}
}