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