Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding unit tests for sorting functions, and improving readability on some sorting algorithms #784

Merged
merged 12 commits into from
May 25, 2019
Next Next commit
Adding variable to fade out ambiguity
  • Loading branch information
alaouimehdi1995 committed May 4, 2019
commit 0a0fe917ddbeaaf2fc93a869b1986544b8e41d28
10 changes: 6 additions & 4 deletions sorts/insertion_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ def insertion_sort(collection):
>>> insertion_sort([-2, -5, -45])
[-45, -5, -2]
"""
for index in range(1, len(collection)):
while index > 0 and collection[index - 1] > collection[index]:
collection[index], collection[index - 1] = collection[index - 1], collection[index]
index -= 1

for loop_index in range(1, len(collection)):
insertion_index = loop_index
while insertion_index > 0 and collection[insertion_index - 1] > collection[insertion_index]:
collection[insertion_index], collection[insertion_index - 1] = collection[insertion_index - 1], collection[insertion_index]
insertion_index -= 1

return collection

Expand Down