We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents df271bf + a681b24 commit 5a14f2cCopy full SHA for 5a14f2c
sorts/insertion_sort.py
@@ -29,14 +29,10 @@ def insertion_sort(collection):
29
>>> insertion_sort([-2, -5, -45])
30
[-45, -5, -2]
31
"""
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
+ 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
40
41
return collection
42
0 commit comments