I couldn't figure out why my merge sort isn't working.
public static void merge(ArrayList<Coupon> arraylist, int LF, int LL, int RF, int RL) {
ArrayList<Coupon> tempArr = arraylist;
int i = LF;
while (LF <= LL && RF <= RL) {
if (arraylist.get(LF).getPrice() < arraylist.get(RF).getPrice()) {
tempArr.Swap(i, LF);
LF++;
} else {
tempArr.Swap(i, RF);;
RF++;
}
i++;
}
while (LF <= LL) {
tempArr.Swap(i, LF);
LF++;
i++;
}
while (RF <= RL) {
tempArr.Swap(i, RF);
RF++;
i++;
}
arraylist = tempArr;
}
public static void mergeSort(ArrayList<Coupon> arraylist, int first, int last) {
if (first < last) {
int middle = (first + last) / 2;
mergeSort(arraylist, first, middle);
mergeSort(arraylist, middle + 1, last);
merge(arraylist, first, middle, middle+1, last);
}
}
I made sure I used it in my main class with correct parameters, I made sure the code actually looped through all the while loops and if-else statements(I checked with system out print in each statement), I also made sure that my Swap method is working by using it elsewhere. Can you help me find what went wrong? Thank you so much in advance!
P.S. LF is left first, LL is left last, RF is right first, RL is right last.