Skip to content

Commit f0d1215

Browse files
author
ryuta69
committed
Add comment
1 parent ad92692 commit f0d1215

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

sorts/merge_insertion_sort.py

+73
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,102 @@ def merge(left, right):
6767
if len(collection) <= 1:
6868
return collection
6969

70+
"""
71+
Group the items into two pairs, and leave one element if there is a last odd item.
72+
73+
Example: [999, 100, 75, 40, 10000]
74+
-> [999, 100], [75, 40]. Leave 10000.
75+
"""
7076
two_paired_list = []
7177
has_last_odd_item = False
7278
for i in range(0, len(collection), 2):
7379
if i == len(collection) - 1:
7480
has_last_odd_item = True
7581
else:
82+
"""
83+
Sort two-pairs in each groups.
84+
85+
Example: [999, 100], [75, 40]
86+
-> [100, 999], [40, 75]
87+
"""
7688
if collection[i] < collection[i + 1]:
7789
two_paired_list.append([collection[i], collection[i + 1]])
7890
else:
7991
two_paired_list.append([collection[i + 1], collection[i]])
92+
93+
"""
94+
Sort two_paired_list.
95+
96+
Example: [100, 999], [40, 75]
97+
-> [40, 75], [100, 999]
98+
"""
8099
sorted_list_2d = sortlist_2d(two_paired_list)
100+
101+
"""
102+
40 < 100 is sure because it has already been sorted.
103+
Generate the sorted_list of them so that you can avoid unnecessary comparison.
104+
105+
Example:
106+
group0 group1
107+
40 100
108+
75 999
109+
->
110+
group0 group1
111+
[40, 100]
112+
75 999
113+
"""
81114
result = [i[0] for i in sorted_list_2d]
115+
116+
"""
117+
100 < 999 is sure because it has already been sorted.
118+
Put 999 in last of the sorted_list so that you can avoid unnecessary comparison.
119+
120+
Example:
121+
group0 group1
122+
[40, 100]
123+
75 999
124+
->
125+
group0 group1
126+
[40, 100, 999]
127+
75
128+
"""
82129
result.append(sorted_list_2d[-1][1])
83130

131+
"""
132+
Insert the last odd item left if there is.
133+
134+
Example:
135+
group0 group1
136+
[40, 100, 999]
137+
75
138+
->
139+
group0 group1
140+
[40, 100, 999, 10000]
141+
75
142+
"""
84143
if has_last_odd_item:
85144
pivot = collection[-1]
86145
result = binary_search_insertion(result, pivot)
87146

147+
"""
148+
Insert the remaining items.
149+
In this case, 40 < 75 is sure because it has already been sorted.
150+
Therefore, you only need to insert 75 into [100, 999, 10000] so that you can avoid unnecessary comparison.
151+
152+
Example:
153+
group0 group1
154+
[40, 100, 999, 10000]
155+
^ You don't need to compare with this as 40 < 75 is already sure.
156+
75
157+
->
158+
[40, 75, 100, 999, 10000]
159+
"""
88160
is_last_odd_item_inserted_before_this_index = False
89161
for i in range(len(sorted_list_2d) - 1):
90162
if result[i] == collection[-i]:
91163
is_last_odd_item_inserted_before_this_index = True
92164
pivot = sorted_list_2d[i][1]
165+
# If last_odd_item is inserted before the item's index, you should forward index one more.
93166
if is_last_odd_item_inserted_before_this_index:
94167
result = result[: i + 2] + binary_search_insertion(result[i + 2 :], pivot)
95168
else:

0 commit comments

Comments
 (0)