Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#26 from yyeltsyn/patch-4
Browse files Browse the repository at this point in the history
Update insertion_sort.py
  • Loading branch information
dynamitechetan authored Sep 5, 2016
2 parents df271bf + a681b24 commit 5a14f2c
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions sorts/insertion_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ def insertion_sort(collection):
>>> insertion_sort([-2, -5, -45])
[-45, -5, -2]
"""
length = len(collection)
for i in range(length):
current_item = collection[i]
j = i - 1
while j >= 0 and current_item < collection[j]:
collection[j+1] = collection[j]
j -= 1
collection[j+1] = current_item
for index in range(1, len(collection)):
while 0 < index and collection[index] < collection[index-1]:
collection[index], collection[index-1] = collection[index-1], collection[index]
index -= 1

return collection

Expand Down

0 comments on commit 5a14f2c

Please sign in to comment.