0

Hey there I have been trying to get an insertion sort method to work for a class I'm taking and we have been told to use insertion sort to sort a linked list of integers without using the linked list class already in the Java libraries.

Here is my inner Node class I have made it only singly linked as i don't fully grasp the circular doubly linked list concept yet

public class IntNode
{
  public int value;
  public IntNode next;
}

And here is my insertion sort method in the IntList class

public IntList Insertion()
{
IntNode current = head;

while(current != null)
    {
    for(IntNode next = current; next.next != null; next = next.next)
        {
        if(next.value <= next.next.value)
            {
            int temp = next.value;
            next.value = next.next.value;
                next.next.value = temp;
            }           
        }
    current = current.next;
    }
return this;
}

The problem I am having is it doesn't sort at all it runs through the loops fine but doesn't manipulate the values in the list at all can someone please explain to me what I have done wrong I am a beginner.

3
  • show us some result sample Commented May 7, 2013 at 3:18
  • This doesn't look a lot like insertion sort. Also, your if test seems backwards; are you trying to sort into descending order? Commented May 7, 2013 at 3:22
  • They told us it didn't matter if it sorted descending or ascending which I thought was odd. In the list generation i pass the main method a integer eg 5 and it will generate 5 numbers between 0 and 5. The list is the same passed out as it is in. Commented May 7, 2013 at 3:24

3 Answers 3

1

you need to start each time from the first Node in your list, and the loop should end with the tail of your list -1 like this

 public static IntList Insertion()
{
     IntNode current = head;
     IntNode tail = null;
     while(current != null&& tail != head )
     {
       IntNode next = current;
      for( ; next.next != tail;  next = next.next)
    {
    if(next.value <= next.next.value)
        {
        int temp = next.value;
        next.value = next.next.value;
            next.next.value = temp;
        }
    }
    tail = next;
   current = head;
  }
 return this;

}

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou aymankoo and everyone else who helped you have been awesome!
0

The insertion operation only works if the list being inserted into is already sorted - otherwise you're just randomly swapping elements. To start out, remove an element from the original list and construct a new list out of it - this list only has one element, hence it is sorted. Now proceed to remove the remaining elements from the original list, inserting them into the new list as you go. At the end the original list will be empty and the new list will be sorted.

2 Comments

What would I need to change in my code to achieve this? besides making a new instance of my list class
Your list class will need to be able to add nodes at arbitrary positions. Your while loop should run until the original list is empty; remove a node from the original list, then use the for loop to insert it into the new list. You should not be swapping any values, only removing and inserting nodes.
0

I agree with the Zim-Zam opinion also. The loop invariant of insertion sort also specifies this: "the subarray which is in sorted order". Below is the code, I implemented for insertion sorting in which I created another linked list that contains the element in sorted order:

Node newList=new Node();
        Node p = newList;
        Node temp=newList;
        newList.data=head.data;
        head=head.node;
        while(head!=null)
        {
            if(head.data<newList.data)
            {
                Node newTemp = new Node();
                newTemp.data=head.data;
                newTemp.node=newList;
                newList=newTemp;
                p=newList;
            }   
            else
            {
                while(newList!=null && head.data>newList.data)
                {
                    temp=newList;
                    newList=newList.node;
                }
                Node newTemp = new Node();
                newTemp.data=head.data;
                temp.node=newTemp;
                newTemp.node=newList;
                newList=p;
            }

            head=head.node;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.