-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertion_sort.py
More file actions
19 lines (17 loc) · 1.05 KB
/
Copy pathinsertion_sort.py
File metadata and controls
19 lines (17 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""Code Fragment 7.17: Python code for performing insertion-sort on a positional list."""
from Goodrich.Chapter7.positional_list import PositionalList
def insertion_sort(L: PositionalList):
"""Sort ``PositionalList`` of comparable elements into nondecreasing order."""
if len(L) > 1: # Otherwise, no need to sort it.
marker = L.first()
while marker != L.last():
pivot = L.after(marker) # Next item to place.
value = pivot.element()
if value > marker.element(): # Pivot is already sorted (CAREFUL if value is non-numeric!).
marker = pivot # Pivot becomes new marker.
else: # Must relocate pivot.
walk = marker # Find leftmost item greater than value.
while walk != L.first() and L.before(walk).element() > value:
walk = L.before(walk)
L.delete(pivot)
L.add_before(walk, value) # Reinsert value before walk.