-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Include the Sleep sort #6951
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
Closed
Closed
Include the Sleep sort #6951
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,68 @@ | ||
""" | ||
This is a pure Python implementation of the sleep algorithm, | ||
In general, sleep sort works by starting a separate task for | ||
each item to be sorted, where each task sleeps for an interval | ||
corresponding to the item's sort key, then emits the item. | ||
Items are then collected sequentially in time. | ||
|
||
More info on: https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort | ||
|
||
For doctests run following command: | ||
python -m doctest -v sleep_sort.py | ||
or | ||
python3 -m doctest -v sleep_sort.py | ||
For manual testing run: | ||
python sleep_sort.py | ||
""" | ||
|
||
from threading import Timer | ||
from time import sleep | ||
|
||
|
||
def sleep_sort(collection): | ||
MatheusMuriel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Pure implementation of sleep sort algorithm in Python | ||
|
||
:param collection: some mutable ordered collection with heterogeneous | ||
comparable items inside | ||
:return: the same collection ordered by ascending | ||
|
||
Examples: | ||
>>> sleep_sort([0, 5, 2, 3, 2]) | ||
[0, 2, 2, 3, 5] | ||
>>> sleep_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2]) | ||
True | ||
>>> sleep_sort([]) == sorted([]) | ||
True | ||
>>> sleep_sort([2, 15, 5]) == sorted([2, 15, 5]) | ||
True | ||
>>> sleep_sort([23, 0, 6, 4, 34]) == sorted([23, 0, 6, 4, 34]) | ||
True | ||
""" | ||
result = [] | ||
|
||
def append_result(x): | ||
MatheusMuriel marked this conversation as resolved.
Show resolved
Hide resolved
MatheusMuriel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result.append(x) | ||
|
||
if len(collection) > 0: | ||
mx = collection[0] | ||
for v in collection: | ||
if mx < v: | ||
mx = v | ||
Timer(v, append_result, [v]).start() | ||
|
||
sleep(mx + 1) | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
import time | ||
|
||
doctest.testmod() | ||
|
||
user_input = input("Enter numbers separated by a comma:").strip() | ||
unsorted = [int(item) for item in user_input.split(",")] | ||
start = time.process_time() | ||
print(*sleep_sort(unsorted), sep=",") | ||
print(f"Processing time: {time.process_time() - start}") |
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.