public static void merge(Elem array[], Elem temp[], int low, int mid, int high) {
int insertindex = low;
int lowindex = low;
int highindex = mid+1;
while (insertindex <= high) {
if (lowindex > mid)
temp[insertindex++] = array[highindex++];
else if (highindex > high)
temp[insertindex++] = array[lowindex++];
else if (array[lowindex].key() < array[highindex].key())
temp[insertindex++] = array[lowindex++];
else
temp[insertindex++] = array[highindex++];
}
for (insertindex = low; insertindex <= high; insertindex++)
array[insertindex] = temp[insertindex];
}
public static void mergesort(Elem array[], Elem temp[], int low, int high) {
int mid;
if (low >= high)
return;
mid = (low + high) / 2;
mergesort(array, temp, low, mid);
mergesort(array, temp, mid+1, high);
merge(array, temp, low, mid, high);
}