-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Working binary insertion sort in Python #8024
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f906d2
added binary insertion sort
shricubed 03dca58
added binary insertion sort working
shricubed 3388753
added binary insertion sort working
shricubed f5824bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fac5089
fixed line length issues
shricubed 39770aa
Merge branch 'master' of github.com:shricubed/Python
shricubed 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 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,63 @@ | ||||||||
""" | ||||||||
This is a pure Python implementation of the binary insertion sort algorithm | ||||||||
|
||||||||
For doctests run following command: | ||||||||
python -m doctest -v binary_insertion_sort.py | ||||||||
or | ||||||||
python3 -m doctest -v binary_insertion_sort.py | ||||||||
|
||||||||
For manual testing run: | ||||||||
python binary_insertion_sort.py | ||||||||
""" | ||||||||
|
||||||||
|
||||||||
def binary_insertion_sort(collection): | ||||||||
"""Pure implementation of the binary insertion sort algorithm in Python | ||||||||
:param collection: some mutable ordered collection with heterogeneous | ||||||||
comparable items inside | ||||||||
:return: the same collection ordered by ascending | ||||||||
|
||||||||
Examples: | ||||||||
>>> binary_insertion_sort([0, 4, 1234, 4, 1]) | ||||||||
[0, 1, 4, 4, 1234] | ||||||||
>>> binary_insertion_sort([]) == sorted([]) | ||||||||
True | ||||||||
>>> binary_insertion_sort([-1, -2, -3]) == sorted([-1, -2, -3]) | ||||||||
True | ||||||||
>>> binary_insertion_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c']) | ||||||||
True | ||||||||
>>> import random | ||||||||
>>> collection = random.sample(range(-50, 50), 100) | ||||||||
>>> binary_insertion_sort(collection) == sorted(collection) | ||||||||
True | ||||||||
>>> import string | ||||||||
>>> collection = random.choices(string.ascii_letters + string.digits, k=100) | ||||||||
>>> binary_insertion_sort(collection) == sorted(collection) | ||||||||
True | ||||||||
""" | ||||||||
|
||||||||
|
||||||||
|
||||||||
n = len(collection) | ||||||||
for i in range(1, n): | ||||||||
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
val = collection[i] | ||||||||
low = 0 | ||||||||
high = i - 1 | ||||||||
|
||||||||
while low <= high: | ||||||||
mid = (low + high) // 2 | ||||||||
if val < collection[mid]: | ||||||||
high = mid - 1 | ||||||||
else: | ||||||||
low = mid + 1 | ||||||||
for j in range(i, low, -1): | ||||||||
collection[j] = collection[j - 1] | ||||||||
collection[low] = val | ||||||||
return collection | ||||||||
|
||||||||
|
||||||||
|
||||||||
if __name__ == "__main__": | ||||||||
user_input = input("Enter numbers separated by a comma:\n").strip() | ||||||||
unsorted = [int(item) for item in user_input.split(",")] | ||||||||
print(binary_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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
binary_insertion_sort
. If the function does not return a value, please provide the type hint as:def function() -> None:
Please provide type hint for the parameter:
collection