Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. def selection_linked(a):
  2. # Split the list into the sorted (_front) and unsorted parts.
  3. unsorted = a._front
  4. a._front = None
  5. # Go through each node in the unsorted list and find the max value
  6. # to insert at the front of the sorted list.
  7. while unsorted is not None:
  8. max_prev = None
  9. max_node = unsorted
  10. previous = unsorted
  11. current = max_node._next
  12.  
  13. while current is not None:
  14. if current._data > max_node._data:
  15. max_prev = previous
  16. max_node = current
  17. previous = current
  18. current = current._next
  19. # Remove the max node from the list.
  20. Sorts.swaps += 1
  21.  
  22. if max_prev is None:
  23. unsorted = max_node._next
  24. else:
  25. max_prev._next = max_node._next
  26. # Move the next max node to the front of the sorted list.
  27. max_node._next = a._front
  28. a._front = max_node
  29. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement