Results 1 to 4 of 4
  1. #1
    bubtub24 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Question Insertion Sort for linked list help?

    Hi, I'm writing an Insertion sort for linked lists and I have the sort working but if I add certain numbers to the list it won't work. I cannot figure out what could be causing this error. It sorts [4, 2, 1, 5, 3, 10, 6, 11, 7] perfectly, but if I add a 9 to the end or other numbers it doesn't work:confused: Any help would be g
    [/CODE]
    Last edited by bubtub24; 11-28-2010 at 07:05 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I have the sort working but if I add certain numbers to the list it won't work

    It's time to debug!

    The thing is to see what it is that your program is doing. IDE's offer debuggers, or you can use the poor man's debugger: System.out.println(). (Actually the latter has some advantage because others here can see your code and run it.)

    Java Code:
        /** 
         * Returns the list from first onwards 
         * as a string of info values. 
         */
    static String asString() 
    {
        StringBuilder buf = new StringBuilder();
        for(Node n = first; n != null; n = n.link) 
        {
            buf.append(n.info).append(" ");
        }
        return buf.toString();
    }
    
    // ...
    
    public static <T> void insertSort()
    {
        Node<Integer> current, head, trail;
    
        current = first;
        trail = current;
        head = current.link;
    
        while(current != null)
        {
            System.out.println("head=" + head.info);
            System.out.println("trail=" + trail.info);
            System.out.println("current=" + current.info);
            if(head.info < trail.info)
            {
                System.out.print("Found " + head.info + " out of order: ");
                int temp = head.info;
                deleteNode(temp);
                position(temp);
    
            } 
            else 
            {
                System.out.print(head.info + " OK: ");
            }
            System.out.println(asString() + "\n");
            trail = current;
            head = current.link;
            current = current.link;
        }
    }

    Basically you are looking to see at what point the head, trail, and current values are not what you expect them to be.
    Last edited by pbrockway2; 11-27-2010 at 10:48 PM.

  3. #3
    bubtub24 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the information it was the trail and head that were wrong. It needed a small amount of tweaking now is fixed.:)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. problem with insertion sort???
    By blueduiker in forum New To Java
    Replies: 2
    Last Post: 03-22-2010, 02:17 PM
  2. why doesnt my insertion sort method not work?
    By Jeremy8 in forum New To Java
    Replies: 7
    Last Post: 11-15-2009, 03:56 AM
  3. [SOLVED] Insertion Sort in Linked List
    By taylorp in forum New To Java
    Replies: 10
    Last Post: 03-27-2009, 01:34 AM
  4. Insertion Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:41 PM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •