Update merge_insertion_sort.py (#5833)

* Update merge_insertion_sort.py

Fixes #5774

merge_insertion_sort

Co-Authored-By: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>

* Update merge_insertion_sort.py

Fixes #5774

merge_insertion_sort

Co-Authored-By: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>

* Update merge_insertion_sort.py

Fixes #5774

added permutation range from 0 to 4

Co-Authored-By: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>

* Use `all()`

Co-authored-by: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>
Co-authored-by: John Law <johnlaw.po@gmail.com>
This commit is contained in:
yellowsto 2021-12-16 10:27:15 +01:00 committed by GitHub
parent 65d3cfff2f
commit 6680e435a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,12 @@ def merge_insertion_sort(collection: list[int]) -> list[int]:
>>> merge_insertion_sort([-2, -5, -45])
[-45, -5, -2]
Testing with all permutations on range(0,5):
>>> import itertools
>>> permutations = list(itertools.permutations([0, 1, 2, 3, 4]))
>>> all(merge_insertion_sort(p) == [0, 1, 2, 3, 4] for p in permutations)
True
"""
def binary_search_insertion(sorted_list, item):
@ -160,7 +166,7 @@ def merge_insertion_sort(collection: list[int]) -> list[int]:
"""
is_last_odd_item_inserted_before_this_index = False
for i in range(len(sorted_list_2d) - 1):
if result[i] == collection[-i]:
if result[i] == collection[-1] and has_last_odd_item:
is_last_odd_item_inserted_before_this_index = True
pivot = sorted_list_2d[i][1]
# If last_odd_item is inserted before the item's index,