Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def selection_linked(a):
- # Split the list into the sorted (_front) and unsorted parts.
- unsorted = a._front
- a._front = None
- # Go through each node in the unsorted list and find the max value
- # to insert at the front of the sorted list.
- while unsorted is not None:
- max_prev = None
- max_node = unsorted
- previous = unsorted
- current = max_node._next
- while current is not None:
- if current._data > max_node._data:
- max_prev = previous
- max_node = current
- previous = current
- current = current._next
- # Remove the max node from the list.
- Sorts.swaps += 1
- if max_prev is None:
- unsorted = max_node._next
- else:
- max_prev._next = max_node._next
- # Move the next max node to the front of the sorted list.
- max_node._next = a._front
- a._front = max_node
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement