0

I am having a bit of a problem with a linked list. I am generating 40 random integers using a random object from random class and append them to a linked list. This also uses a specified seed. Everything works perfectly minus this one bug. The first thing being printed in the console/output is the linked list with the randomly generated 40 ints. I am then trying to sort the list using a decreasing insert sort which is where I think my bug lies. My attempt at the "decreasing insert sort algorithm" is done in the isdRecI and isdRecII methods, these are recursive (Be careful large parts of this program are recursive, so if you are not familiar with recursion beware). Once the sort is complete I want to print the linked list again in decreasing order. Please try to keep it as simple as possible and if you can like my style of code since I am somewhat of a Java beginner i.e. Please don't over complicate it. As you can see if you compile my code you will see the duplicates in the sorted print. My code is listed below. I also understand the concept of a linked list and the insert sort quite well, but I am having a rough time getting the code to output the way I want. Please feel free to modify my methods. Thank you for your time and contribution.

public class Node 
{

private int data = 0; 
Node next; 

public Node (int datax)  //constructor 
{
    data = datax; 
    next = null;
}

public int getData()  // get the data value
{
    return data;
}

public void setData(int datax) // sets the data value   
{
    data = datax ; 
}

public void print() // print node data on one line.
{
    System.out.print(data + "  ");
}

public Node getNext()
{
    return (next);
}

public void setNext(Node nextx)
{
    next = nextx; 
}
} 

import java.util.Random;

public class MySort 

{
Node head; 


/*
*  This method appends iteratively to the end of the linked list
*/
public void appendIter(int datax) 
{
    Node newNode = new Node(datax); 
    Node rightpointer = head; 
    if (rightpointer == null) 
    {
        head = newNode;
        return; 
    }
    else 
    {   
        while (rightpointer.getNext() != null) 
        {
            rightpointer = rightpointer.getNext(); 
        } 
        rightpointer.setNext(newNode);  

    }
}

/* 
 * This method passes the data to isdRecI
 */

        public void isRecI(MySort unsortedList)
    {
        isRecII(head);
    }

    public void isRecII(Node unsortedPointer)
    {
        int data;
        if(unsortedPointer == null)
        {
            return;
        }
        data = unsortedPointer.getData();
        isdRecI(data);
        isRecII(unsortedPointer.getNext());
    }


   /*
    * This method sorts the data using insert sort and sorts in decreasing order  
    * 
    */
    public void isdRecI(int dx)
    {
        head = isdRecII(head, dx);
    }

    public Node isdRecII(Node hp, int dx)
    {
        Node nxp;
        /*
        if(hp == null)
        {
            nxp = new Node(dx);  // commented out for testing purposes please uncomment if you need 
            return nxp;
        }
        */
       if(dx >=  hp.getData())    
        {   
            nxp = new Node(dx);
            nxp.setNext(hp);
            return nxp;
        }
        hp.setNext(isdRecII(hp.getNext(),dx));
        return hp;
    }

    /*
     * This method is an iterative print method for the linked list 
     */
    public void print()
    {
        System.out.println("print list start: ");
        Node nextrightpointer = head;
        while (nextrightpointer != null)
        {
            nextrightpointer.print();
            nextrightpointer = nextrightpointer.getNext();
        }
      System.out.println("print list end");
    }


    public static void main(String[] args)
{
    MySort SortObject = new MySort ();
    Random random = new Random(12345);

    for(int i=0; i < 40;i++)
        {
            SortObject.appendIter(random.nextInt(200));
        }

        SortObject.print();
        SortObject.isRecI(SortObject); 
        System.out.println();
        SortObject.print(); 

}
}

Also including the output:

print list start: 51 80 41 28 55 84 175 2 101 189 117 142 190 6 12 184 187 103 132 175 1 151 192 116 28 181 25 143 71 39 129 197 101 25 103 155 152 31 10 108 print list end

(After sort // this is not printed just FYI)

print list start: 197 192 190 189 187 184 181 175 175 155 152 151 143 142 132 129 117 116 108 103 103 101 101 84 80 71 55 51 51 80 41 41 39 31 28 28 28 55 84 175 25 25 12 10 6 2 2 101 189 117 142 190 6 12 184 187 103 132 175 1 1 151 192 116 28 181 25 143 71 39 129 197 101 25 103 155 152 31 10 108 print list end

Jean
  • 1
  • 1
  • Is this a homework problem? If so you should tag it as such. – davmac Apr 05 '11 at 03:03
  • It is not a homework problem I was just testing myself to see if I am able to complete it, although I did pull it from an academic source. – Jean Apr 05 '11 at 03:37

3 Answers3

0

What sorting algorithm are you attempting to implement? I would recommend looking at a standard sort algorithm (e.g. bubblesort, mergesort), and get that working. From your description, it looks like you just came up with your own sort algorithm (not recommended). Java also has built in mechanisms to help you sort your data. For example, you can override the compareTo method on your Node class and call Collections.sort() on your list.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • The sorting algorithm that I am attempting to implement is the "insert sort" I only have part of the insert sort implemented. I understand the compareTo method slightly but want to solve this using the "insert sort." Thanks – Jean Apr 05 '11 at 03:07
0

Your problem is that "head" refers to the unsorted list, but in the isdRecI method it is treated as if it describes the sorted list. The first thing isdRecI does is add a new node to the list - so the list now contains the original unsorted list, plus a new node.

davmac
  • 20,150
  • 1
  • 40
  • 68
0

1) You have way too many recursive functions. One main function and one helper should suffice. 2) You call to add a new value to the list without removing it. I'll try and touch up the recursive functions and post what I get.

Here's the new code I got. Since you didn't implement a Doubly Linked List, I had to do some cheese in order to add/remove.

public void isRecI(Node unsortedList)
{
    int data;
    if(unsortedList == null)
    {
        return;
    }
    Node prev = unsortedList;
    unsortedList = unsortedList.getNext(); //first element is in order
    while(unsortedList != null){
        reHelper(head , null , unsortedList , prev);
        prev = unsortedList;
        unsortedList = unsortedList.getNext();
    }
}

public void reHelper(Node inOrder , Node orderPrev , Node toInsert , Node insertPrev){
    if(inOrder == toInsert)return;
    if(inOrder.getData() < toInsert.getData()){
        insertPrev.setNext(toInsert.getNext()); //remove from list
        toInsert.setNext(inOrder);// first part of add to list
        if(orderPrev == null){
            head = toInsert; // make head
        }
        else{
            orderPrev.setNext(toInsert);// finish add to list
        }
        return;
    }
    reHelper(inOrder.getNext() , inOrder , toInsert , insertPrev);
}
Duncan
  • 980
  • 6
  • 17
  • Thank you duncan. I am was looking into the idea of removing the items from the list after sorting. Please post what you get, I would really appreciate it. – Jean Apr 05 '11 at 03:35
  • For some reason I can't comment on others' posts, so I'll comment here. In order to call Collections.sort() your Linked List would have to implement the List interface and incorporate all methods it specifies. – Duncan Apr 05 '11 at 04:00