I have a class that does some recursive merge sorting on a generic List, as long as the element implement Comparable. I have a void method called mergeSort(List toSort), and a method mergeSortedLists(List left, List right) that takes two lists that have already been sorted, and then combines them into one sorted list. The problem is, the mergeSort(...) method doesn't seem to be manipulating the toSort variable. Well it is, but the changes don't show after it jumps up a level. Here are the sorting methods:
public static <E extends Comparable<E>> void mergeSort(List<E> toSort)
{
if(toSort.size() > 1)
{
List<E> temp = toSort.subList(0, toSort.size()/2);
ArrayList<E> left = new ArrayList<E>(0);
for(E e : temp) left.add(e);
temp = toSort.subList(toSort.size()/2, toSort.size());
ArrayList<E> right = new ArrayList<E>(0);
for(E e : temp) right.add(e);
if(right.size() != 1) mergeSort(right);
if(left.size() != 1) mergeSort(left);
toSort = mergeSortedLists(left, right);
}
}
public static <E extends Comparable<E>> List<E> mergeSortedLists(List<E> leftList, List<E> rightList)
{
ArrayList<E> list = new ArrayList<E>();
while(!leftList.isEmpty() && !rightList.isEmpty())
{
if((leftList.get(0)).compareTo(rightList.get(0)) <= 0)
list.add(leftList.remove(0));
else
list.add(rightList.remove(0));
}
while(!leftList.isEmpty())
list.add(leftList.remove(0));
while(!rightList.isEmpty())
list.add(rightList.remove(0));
return list;
}
I normally have print statements for error checking, and those show that mergeSortedLists(...) sorts correctly and returns the correct list. Then, I assign the toSort variable in mergeSort(...) to whatever mergeSortedLists(...) returns. That assignment works. Now, it jumps back up a level to combine that list with a different list, and the changes seem to be lost. I have no clue what is happening.