Skip to content

Commit 597f71a

Browse files
author
ryuta69
committed
Add merge insertion sort
1 parent 88e82db commit 597f71a

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

sorts/merge_insertion_sort.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
This is a pure Python implementation of the quick sort algorithm
3+
4+
For doctests run following command:
5+
python -m doctest -v merge_insertion_sort.py
6+
or
7+
python3 -m doctest -v merge_insertion_sort.py
8+
9+
For manual testing run:
10+
python merge_insertion_sort.py
11+
"""
12+
13+
14+
def mergeInsertionSort(collection):
15+
"""Pure implementation of merge-insertion sort algorithm in Python
16+
17+
:param collection: some mutable ordered collection with heterogeneous
18+
comparable items inside
19+
:return: the same collection ordered by ascending
20+
21+
Examples:
22+
>>> mergeInsertionSort([0, 5, 3, 2, 2])
23+
[0, 2, 2, 3, 5]
24+
25+
>>> mergeInsertionSort([])
26+
[]
27+
28+
>>> mergeInsertionSort([-2, -5, -45])
29+
[-45, -5, -2]
30+
"""
31+
32+
def binary_search_insertion(sorted_list, item):
33+
left = 0
34+
right = len(sorted_list) - 1
35+
while left <= right:
36+
middle = (left + right) // 2
37+
if left == right:
38+
if sorted_list[middle] < item:
39+
left = middle + 1
40+
break
41+
else:
42+
break
43+
elif sorted_list[middle] < item:
44+
left = middle + 1
45+
else:
46+
right = middle - 1
47+
sorted_list.insert(left, item)
48+
return sorted_list
49+
50+
def sortlist_2d(list_2d):
51+
def merge(left, right):
52+
result = []
53+
while left and right:
54+
if left[0][0] < right[0][0]:
55+
result.append(left.pop(0))
56+
else:
57+
result.append(right.pop(0))
58+
return result + left + right
59+
60+
length = len(list_2d)
61+
if length <= 1:
62+
return list_2d
63+
middle = length // 2
64+
return merge(sortlist_2d(list_2d[:middle]), sortlist_2d(list_2d[middle:]))
65+
66+
if len(collection) <= 1:
67+
return collection
68+
69+
two_paired_list = []
70+
is_surplus = False
71+
for i in range(0, len(collection), 2):
72+
if i == len(collection) - 1:
73+
is_surplus = True
74+
else:
75+
if collection[i] < collection[i + 1]:
76+
two_paired_list.append([collection[i], collection[i + 1]])
77+
else:
78+
two_paired_list.append([collection[i + 1], collection[i]])
79+
sorted_list_2d = sortlist_2d(two_paired_list)
80+
result = [i[0] for i in sorted_list_2d]
81+
result.append(sorted_list_2d[-1][1])
82+
83+
if is_surplus:
84+
pivot = collection[-1]
85+
result = binary_search_insertion(result, pivot)
86+
87+
is_surplus_inserted_before_this_index = False
88+
for i in range(len(sorted_list_2d) - 1):
89+
if result[i] == collection[-i]:
90+
is_surplus_inserted_before_this_index = True
91+
pivot = sorted_list_2d[i][1]
92+
if is_surplus_inserted_before_this_index:
93+
result = result[: i + 2] + binary_search_insertion(result[i + 2 :], pivot)
94+
else:
95+
result = result[: i + 1] + binary_search_insertion(result[i + 1 :], pivot)
96+
97+
return result
98+
99+
100+
if __name__ == "__main__":
101+
user_input = input("Enter numbers separated by a comma:\n").strip()
102+
unsorted = [int(item) for item in user_input.split(",")]
103+
print(mergeInsertionSort(unsorted))

0 commit comments

Comments
 (0)