: : Hullo, heres my problems I need to write a mergesort variant for class that works with LinkedLists instead of arrays. Ive got the code down fine but heres the problem. When i call a "split" method to split the Lists
: :
: : public void split(LinkedList listA, LinkedList listB){
: : ...
: : }
: :
: :
: : public LinkedList mergeSort(LinkedList listA){
: : if(listA.size()<1)
: : return listA;
: : if(listA.size()==0)
: : return null;
: : LinkedList listB=new LinkedList();
: : split(listA, listB);
: : //Got an outputting debug line here
: : return merge(mergeSort(listA),mergeSort(listB));
: : }
: :
: : Don't even bother with the merge method, because its not even going their
because the lists arent being split. Its running infnitely with the lists never getting smaller. I know that its because when i change the lists in the split method the mergeSort method doesnt see these changes, but how can I "pass them by reference" to borrow the C++ terminology? I also can't change it to just put the split code inside mergeSort, was given to me as an assignment like this. Thx in adavnace,
: : Troubled,Confused, and a little hungry
: :
:
:
: Why do you have these two conditionals?
:
: if(listA.size()<1)
: return listA;
: if(listA.size()==0)
: return null;
:
: Assuming your list can't have a negative size(which wouldn't make sense), they do the same thing, return if the list is size 0.
:
: What does the code for split look like? It is tad hard to tell you why the lists aren't being split when you don't post the code. :)
:
: Remember that LL are not like arrays, you have to 'walk' out to the middle of the list.
:
yea ive made sure to walk out in the split code..here it is for further clarification
public void split(LinkedList listA, LinkedList listB)
{
LinkedList tA=new LinkedList();
LinkedList tB=new LinkedList();
int first=0, last=listA.size()-1;
int mid=(first+last)/2;
for(int i=first;i<=mid;i++)
tA.addLast(listA.get(i));
for(int j=mid+1;j<=last;j++)
tB.addLast(listA.get(j));
listA=tA;
listB=tB;
}
ive tested the split code by itself and it does work properly, that is not the problem.
Oh the <1 was a typo on my part...it is an equals sign in the true code