-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add merge insertion sort #2211
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
Merged
Merged
Add merge insertion sort #2211
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
597f71a
Add merge insertion sort
5b94776
Fix python naming conventions
50c4185
Add wikipedia link
23adf96
Add type hint
880b0ac
Fix python to python3
219a4c8
Refactor doubled process in if-condition into one outside of if-condi…
b7b66c3
Refactor make python3 prior to python
ad92692
Fix name of is_surplus into has_last_odd_item
f0d1215
Add comment
8ed76fa
Fix long comment to shorten
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
""" | ||
This is a pure Python implementation of the quick sort algorithm | ||
|
||
For doctests run following command: | ||
python -m doctest -v merge_insertion_sort.py | ||
or | ||
python3 -m doctest -v merge_insertion_sort.py | ||
|
||
For manual testing run: | ||
python merge_insertion_sort.py | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
|
||
def merge_insertion_sort(collection): | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Pure implementation of merge-insertion sort algorithm in Python | ||
|
||
:param collection: some mutable ordered collection with heterogeneous | ||
comparable items inside | ||
:return: the same collection ordered by ascending | ||
|
||
Examples: | ||
>>> merge_insertion_sort([0, 5, 3, 2, 2]) | ||
[0, 2, 2, 3, 5] | ||
|
||
>>> merge_insertion_sort([]) | ||
[] | ||
|
||
>>> merge_insertion_sort([-2, -5, -45]) | ||
[-45, -5, -2] | ||
""" | ||
|
||
def binary_search_insertion(sorted_list, item): | ||
left = 0 | ||
right = len(sorted_list) - 1 | ||
while left <= right: | ||
middle = (left + right) // 2 | ||
if left == right: | ||
if sorted_list[middle] < item: | ||
left = middle + 1 | ||
break | ||
else: | ||
break | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
elif sorted_list[middle] < item: | ||
left = middle + 1 | ||
else: | ||
right = middle - 1 | ||
sorted_list.insert(left, item) | ||
return sorted_list | ||
|
||
def sortlist_2d(list_2d): | ||
def merge(left, right): | ||
result = [] | ||
while left and right: | ||
if left[0][0] < right[0][0]: | ||
result.append(left.pop(0)) | ||
else: | ||
result.append(right.pop(0)) | ||
return result + left + right | ||
|
||
length = len(list_2d) | ||
if length <= 1: | ||
return list_2d | ||
middle = length // 2 | ||
return merge(sortlist_2d(list_2d[:middle]), sortlist_2d(list_2d[middle:])) | ||
|
||
if len(collection) <= 1: | ||
return collection | ||
|
||
two_paired_list = [] | ||
is_surplus = False | ||
for i in range(0, len(collection), 2): | ||
if i == len(collection) - 1: | ||
is_surplus = True | ||
else: | ||
if collection[i] < collection[i + 1]: | ||
two_paired_list.append([collection[i], collection[i + 1]]) | ||
else: | ||
two_paired_list.append([collection[i + 1], collection[i]]) | ||
sorted_list_2d = sortlist_2d(two_paired_list) | ||
result = [i[0] for i in sorted_list_2d] | ||
result.append(sorted_list_2d[-1][1]) | ||
|
||
if is_surplus: | ||
pivot = collection[-1] | ||
result = binary_search_insertion(result, pivot) | ||
|
||
is_surplus_inserted_before_this_index = False | ||
for i in range(len(sorted_list_2d) - 1): | ||
if result[i] == collection[-i]: | ||
is_surplus_inserted_before_this_index = True | ||
pivot = sorted_list_2d[i][1] | ||
if is_surplus_inserted_before_this_index: | ||
result = result[: i + 2] + binary_search_insertion(result[i + 2 :], pivot) | ||
else: | ||
result = result[: i + 1] + binary_search_insertion(result[i + 1 :], pivot) | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
user_input = input("Enter numbers separated by a comma:\n").strip() | ||
unsorted = [int(item) for item in user_input.split(",")] | ||
print(merge_insertion_sort(unsorted)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.