Help with Recursive Merge Sort in Java Using Linked Lists

vayanui8

Well-Known Member
OP
Member
Joined
Nov 11, 2013
Messages
1,086
Trophies
0
XP
908
Country
United States
I'm currently working on programming a recursive mergesort method for my comp sci class. We are required to use Linked Lists for this method, and as I'm pretty terrible at both recursion and sorting algorithms this assignment has been giving me alot of trouble. We were given some starter code to get an idea, but I don't think my methods work properly, and Im getting a stack overflow error. Could somebody please help me?



Code:
import java.util.*;
import java.io.*;

/**
 *  Implements a recursive mergesort on a LinkedList data type
 *
 * [USER=353451]@Author[/USER]  G. Peck
 * @created  July 18, 2002
 *
 * Modified by Jason Quesenberry and Nancy Quesenberry
 * February 9, 2006
 */
public class MergeList{
  private  Scanner inFile;
  private String myFile;

  /**
  *  Open a file containing id/inventory pairs of data
  *
  * @param  fileName  File to be opened
  */
  public MergseList(String fileName)
  {
  try{
  inFile = new Scanner(new File(fileName));
  }
  catch(IOException i){
  System.out.println("Error: " + i.getMessage());
  }
  myFile = fileName;
  }

  /**
  *  Reads a file containing id/inv data pairs. The first line of the
  *  file contains the number of id/inventory integer pairs listed on
  *  subsequent lines.
  *
  * @param  list  Reference to LinkedList to which data will be added
  */
  public void readData(LinkedList<Item> list){
  int id = 0;
  int inv = 0;
  LinkedList<Item> tempList = new LinkedList<Item>();
  int count = 0;
  while (inFile.hasNext())
  {
  count++;
  if(count%2 == 0)
  {
  inv = Integer.parseInt(inFile.next()
);
  tempList.addFirst(new Item(id, inv));
  }
  else
  {
  id = Integer.parseInt(inFile.next());
  }  
  }  
  list = tempList;
  }

  /**
  *  Prints contents of list
  *
  * @param  list  linked list to be printed
  */
  public void printList(LinkedList list)
  {
  System.out.printf("%5s%16s","Id","Inventory\n");
  ListIterator<Item> listIterator = list.listIterator();  
  while(listIterator.hasNext())
  {
  Item harman = listIterator.next();
  System.out.printf("%5s%16s", harman.getId() , harman.getInv());
  System.out.println();   
  }
  System.out.println(); 
  System.out.println();
  }

  /**
  *  Splits listA into two parts. listA retains the first
  *  half of the list, listB contains the last half of
  *  the original list.
  *
  * @param  listA  Original list. reduced by half after split.
  * @param  listB  Contains last half of the original list
  */
  public void split(LinkedList<Item> listA, LinkedList<Item> listB)
  {  
  int temp = listA.size()/2;
  while (temp<listA.size())
  {
  listA.removeLast();
  listB.removeFirst();
  temp++;
  }

  }

  /**
  *  Two linked lists listA and listB are merged into a single
  *  linked list mergedList. They are placed in mergedList starting
  *  with the smallest item on either list and continue working up to
  *  to largest item.
  *
  * @param  listA  LinkedList in nondecreasing order
  * @param  listB  LinkedList in nondecreasing order
  * @return  List  containing all the values from lists listA
  *  and listB, in nondecreasing order
  */
  public LinkedList<Item> merge(LinkedList<Item> listA, LinkedList<Item> listB){
  // make sure the target list is empty
  LinkedList <Item> mergedList = new LinkedList <Item>();  
  ListIterator<Item> listIterator1 = listA.listIterator();  
  ListIterator<Item> listIterator2 = listB.listIterator();  
  while(listIterator1.hasNext() && listIterator2.hasNext())
  {
  Item jim = listIterator1.next();
  Item jam = listIterator2.next();
  if(jim.compareTo(jam) <= 0)
  {
  mergedList.addLast(jam);
  }
  else 
  {
  mergedList.addLast(jim);
  }
  }
  while(listIterator1.hasNext())
  {
  mergedList.addLast(listIterator1.next());
  }
  while(listIterator2.hasNext())
  {

  mergedList.addLast(listIterator2.next());

  }
  // start at mergedList left and right item
  // continue until either left or right list has no more value to add
  // use <= instead of < so if duplicate take from left so sort is stable

  // One of the next two while loops will execute.  It will be the one with some values
  // left on the list.
  return mergedList;
  }

  /***  The linked list is returned in sorted order.
  *  Sorted order has the smallest item first and the largest item
  *  last. The ordering is determined by the order defined in the
  *  Comparable class. The method uses the merge sort technique and
  *  must be recursive.
  *
  * @param  listA  LinkedList to be sorted
  * @return  LinkedList in sorted (nondecreasing) order
  */
  public LinkedList<Item> mergeSort(LinkedList <Item> listA){
  LinkedList <Item> listB =  (LinkedList) listA.clone();
  // Split the list in half.  If uneven then make left one larger.
  split(listA, listB);
  return merge(mergeSort(listA), mergeSort(listB));
  }

  /**
  *  Reverses the order of a list
  *
  * @param  list  LinkedList to be reversed
  * @return  List in reverse order
  */
  public LinkedList<Item> reverseList(LinkedList <Item>list)
  { 
  LinkedList <Item> listB =  new LinkedList<Item>();
  ListIterator<Item> listIterator = list.listIterator();  
  while(listIterator.hasNext())
  {
  listB.addLast(list.getFirst());
  }
  return listB;
  }
}
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • Psionic Roshambo @ Psionic Roshambo:
    Megatron "Prime why throw away your life!" Prime "How do I think I got these gold rims!!!"
  • Psionic Roshambo @ Psionic Roshambo:
    Autobots roll some blunts and smoke out!
  • Psionic Roshambo @ Psionic Roshambo:
    Channel 69 news "Last night someone stole several cars and a semi truck loaded with drugs from the police impound lot!"
  • BakerMan @ BakerMan:
    i'mma @ juan in the joke thread and say "hey juan i call your mom dwayne the way she rock my johnson"
  • Psionic Roshambo @ Psionic Roshambo:
    Prowl was the autobots inside man... lol
  • Jayro @ Jayro:
    Is GBAtemp being a sluggish turd for anyone else today, or just me..?
  • K3Nv2 @ K3Nv2:
    Goku likes to flip the bean to get others healthy
  • SylverReZ @ SylverReZ:
    @Jayro, No, works fine.
  • SylverReZ @ SylverReZ:
    Hope your weekend's great. :)
  • SylverReZ @ SylverReZ:
    My friend found this the other day. Courtesy of @Kaoid
  • SylverReZ @ SylverReZ:
    Sylveon popping out of a DS lite.
  • DinohScene @ DinohScene:
    pretty decent considering
    +1
  • DinohScene @ DinohScene:
    just under 2 weeks orso before the new harddrive arrives
    +1
  • SylverReZ @ SylverReZ:
    @DinohScene, How's that hard drive recovery project going?
  • DinohScene @ DinohScene:
    which one
  • SylverReZ @ SylverReZ:
    @DinohScene, You mentioned that you have a new hard drive coming.
  • DinohScene @ DinohScene:
    yeh in 2 weeks orso
  • SylverReZ @ SylverReZ:
    Do you have a NAS storage?
  • K3Nv2 @ K3Nv2:
    illmatic
  • DinohScene @ DinohScene:
    39 TB server
  • SylverReZ @ SylverReZ:
    @DinohScene, Jeezus. 39TB sounds like a significant investment of time.
  • DinohScene @ DinohScene:
    meh, more of a I need to add bigger drives all the time
    DinohScene @ DinohScene: meh, more of a I need to add bigger drives all the time