Skip to content

Commit 6680e43

Browse files
yellowstoAilisOsswaldpoyea
authored
Update merge_insertion_sort.py (TheAlgorithms#5833)
* Update merge_insertion_sort.py Fixes TheAlgorithms#5774 merge_insertion_sort Co-Authored-By: AilisOsswald <[email protected]> * Update merge_insertion_sort.py Fixes TheAlgorithms#5774 merge_insertion_sort Co-Authored-By: AilisOsswald <[email protected]> * Update merge_insertion_sort.py Fixes TheAlgorithms#5774 added permutation range from 0 to 4 Co-Authored-By: AilisOsswald <[email protected]> * Use `all()` Co-authored-by: AilisOsswald <[email protected]> Co-authored-by: John Law <[email protected]>
1 parent 65d3cff commit 6680e43

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Diff for: sorts/merge_insertion_sort.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def merge_insertion_sort(collection: list[int]) -> list[int]:
3030
3131
>>> merge_insertion_sort([-2, -5, -45])
3232
[-45, -5, -2]
33+
34+
Testing with all permutations on range(0,5):
35+
>>> import itertools
36+
>>> permutations = list(itertools.permutations([0, 1, 2, 3, 4]))
37+
>>> all(merge_insertion_sort(p) == [0, 1, 2, 3, 4] for p in permutations)
38+
True
3339
"""
3440

3541
def binary_search_insertion(sorted_list, item):
@@ -160,7 +166,7 @@ def merge(left, right):
160166
"""
161167
is_last_odd_item_inserted_before_this_index = False
162168
for i in range(len(sorted_list_2d) - 1):
163-
if result[i] == collection[-i]:
169+
if result[i] == collection[-1] and has_last_odd_item:
164170
is_last_odd_item_inserted_before_this_index = True
165171
pivot = sorted_list_2d[i][1]
166172
# If last_odd_item is inserted before the item's index,

0 commit comments

Comments
 (0)