mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
Update insertion_sort.py
Cleaner solution: shorter, less variables, straightforward logic.
This commit is contained in:
parent
df271bf2d0
commit
a681b24f69
|
@ -29,14 +29,10 @@ def insertion_sort(collection):
|
||||||
>>> insertion_sort([-2, -5, -45])
|
>>> insertion_sort([-2, -5, -45])
|
||||||
[-45, -5, -2]
|
[-45, -5, -2]
|
||||||
"""
|
"""
|
||||||
length = len(collection)
|
for index in range(1, len(collection)):
|
||||||
for i in range(length):
|
while 0 < index and collection[index] < collection[index-1]:
|
||||||
current_item = collection[i]
|
collection[index], collection[index-1] = collection[index-1], collection[index]
|
||||||
j = i - 1
|
index -= 1
|
||||||
while j >= 0 and current_item < collection[j]:
|
|
||||||
collection[j+1] = collection[j]
|
|
||||||
j -= 1
|
|
||||||
collection[j+1] = current_item
|
|
||||||
|
|
||||||
return collection
|
return collection
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user