Merge Sort
//TODO: implement it again
public class MergeSort {
private static int[] helperArray;
public MergeSort(){}
public static void sort(int[] array) {
helperArray = new int[array.length];
mergesort(array, 0, array.length - 1);
}
private static void mergesort(int[] array, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergesort(array, low, mid);
mergesort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
private static void merge(int[] array, int low, int mid, int high) {
for (int i = low; i <= high; i++) {
helperArray[i] = array[i];
}
int helperLeft = low;
int helperRight = mid + 1;
int current = low;
while (helperLeft <= mid && helperRight <= high) {
if (helperArray[helperLeft] <= helperArray[helperRight]) {
array[current] = helperArray[helperLeft];
helperLeft++;
} else {
array[current] = helperArray[helperRight];
helperRight++;
}
current++;
}
// We only have to copy the remaining elements from the left half of the helper array into the target array.
// The right half doesn't need to because it's already there
while (helperLeft <= mid) {
array[current] = helperArray[helperLeft];
helperLeft++;
current++;
}
}
}
Count Invert
public class MergeSortAndCountInvert {
private static int[] helperArray;
private static long numInversions = 0;
public static long numInvert(int[] array) {
helperArray = new int[array.length];
mergeSortAndCountInvert(array, 0, array.length - 1);
return numInversions;
}
private static void mergeSortAndCountInvert(int[] array, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergeSortAndCountInvert(array, low, mid);
mergeSortAndCountInvert(array, mid + 1, high);
mergeAndCount(array, low, mid, high);
}
}
private static void mergeAndCount(int[] array, int low, int mid, int high) {
for (int i = low; i <= high; i++) {
helperArray[i] = array[i];
}
int helperLeft = low;
int helperRight = mid + 1;
int current = low;
while (helperLeft <= mid && helperRight <= high) {
if (helperArray[helperLeft] <= helperArray[helperRight]) {
array[current] = helperArray[helperLeft];
helperLeft++;
} else {
// count number of inversions
numInversions += (mid - helperLeft + 1);
array[current] = helperArray[helperRight];
helperRight++;
}
current++;
}
while (helperLeft <= mid) {
array[current] = helperArray[helperLeft];
helperLeft++;
current++;
}
}
}