0

I am trying to create an insertion sort with linked lists. Here is what I have:

def insertion_sort(a):
        """
        -------------------------------------------------------
        Sorts a list using the Insertion Sort algorithm.
        Use: insertion_sort( a )
        -------------------------------------------------------
        Preconditions:
          a - linked list of comparable elements (?)
        Postconditions:
          Contents of a are sorted.
        -------------------------------------------------------
        """        
        unsorted = a._front
        a._front = None

        while unsorted is not None and unsorted._next is not None:
            current = unsorted
            unsorted = unsorted._next

            if current._value < unsorted._value:
                current._next = unsorted._next
                unsorted._next = current
                unsorted = unsorted._next
            else:
                find = unsorted
                while find._next is not None and current._value > find._next._value:
                    find = find._next

                current._next = find._next
                current = find._next
            a._front = unsorted

        return a

I believe what I have is correct in terms of sorting. However when I try to read the list in the main module I get a bunch of None values.

In this case, the insertion sort is not creating a new list when sorting. Rather, it is moving all sorted elements to the 'front'.

To summarize, I have two problems: I am not sure if the insertion sort is correct, and there are problems with the returned list a as it contains None values. Thanks in advance

1 Answer 1

2

Not exactly sure about the type of a is, but if you assume a simple:

class Node:
    def __init__(self, value, node=None):
        self._value = value
        self._next = node
    def __str__(self):
        return "Node({}, {})".format(self._value, self._next)

Then your insertion sort isn't far off, it needs to handle the head case properly:

def insertion_sort(unsorted):    
    head = None
    while unsorted:
        current = unsorted
        unsorted = unsorted._next
        if not head or current._value < head._value:
            current._next = head;
            head = current;
        else:
            find = head;
            while find and current._value > find._next._value:
                find = find._next
            current._next = find._next
            find._next = current
    return head

>>> print(insertion_sort(Node(4, Node(1, Node(3, Node(2))))))
Node(1, Node(2, Node(3, Node(4, None))))
Sign up to request clarification or add additional context in comments.

2 Comments

a is a linked list passed on from the main module. Does this change anything
if a has a similar structure to above then should be fine, the algorithm works so you just need to fit your structure to it.

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.