Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5a14f2c

Browse files
authoredSep 5, 2016
Merge pull request TheAlgorithms#26 from yyeltsyn/patch-4
Update insertion_sort.py
2 parents df271bf + a681b24 commit 5a14f2c

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed
 

‎sorts/insertion_sort.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ def insertion_sort(collection):
2929
>>> insertion_sort([-2, -5, -45])
3030
[-45, -5, -2]
3131
"""
32-
length = len(collection)
33-
for i in range(length):
34-
current_item = collection[i]
35-
j = i - 1
36-
while j >= 0 and current_item < collection[j]:
37-
collection[j+1] = collection[j]
38-
j -= 1
39-
collection[j+1] = current_item
32+
for index in range(1, len(collection)):
33+
while 0 < index and collection[index] < collection[index-1]:
34+
collection[index], collection[index-1] = collection[index-1], collection[index]
35+
index -= 1
4036

4137
return collection
4238

0 commit comments

Comments
 (0)
Please sign in to comment.