Files
AlgorithmNotes/Foundations/overview/insertion_sort.py
pezy_mbp c9d631140f added 2.1-1,2,3
1. added insertion-sort pseudocode and code,unittest
2. added 2.1-1 and 2.1-2
3. part of 2.1-3
2015-05-11 01:09:00 +08:00

23 lines
492 B
Python

__author__ = 'pezy'
def insertion_sort(lst):
for j in range(1, len(lst)):
key = lst[j]
i = j - 1
while i >= 0 and lst[i] > key:
lst[i + 1] = lst[i]
i -= 1
lst[i + 1] = key
return lst
def insertion_sort_non_increasing(lst):
for j in range(1, len(lst)):
key = lst[j]
i = j - 1
while i >= 0 and lst[i] < key:
lst[i + 1] = lst[i]
i -= 1
lst[i + 1] = key
return lst