-
-
Notifications
You must be signed in to change notification settings - Fork 50.1k
Add sorts/sleep_sort.py #13507
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
NghiemNgocDuc
wants to merge
3
commits into
TheAlgorithms:master
from
NghiemNgocDuc:sorting/sleep-sort
Closed
Add sorts/sleep_sort.py #13507
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from typing import List | ||
| import threading | ||
| import time | ||
|
|
||
|
|
||
| def sleep_sort(numbers: List[int], simulate: bool = True, scale: float = 0.01) -> None: | ||
| """ | ||
| Perform Sleep Sort on the given list of integers. | ||
|
|
||
| Sorts the list numbers in place using the Sleep Sort algorithm. | ||
|
|
||
| Behavior: | ||
| - Destructive: modifies the list numbers. | ||
| - Only accepts integers (positive, zero, or negative). | ||
| - Default simulate=True runs instantly by simulating timed wake-ups (safe for tests/CI). | ||
| - If simulate=False, the function spawns one thread per element and uses time.sleep; | ||
| this mode causes real waiting time proportional to element values. | ||
| - scale (seconds per unit) applies only when simulate=False. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> nums = [3, 1, 2] | ||
| >>> sleep_sort(nums) | ||
| >>> nums | ||
| [1, 2, 3] | ||
|
|
||
| >>> nums = [0, 0, 1] | ||
| >>> sleep_sort(nums) | ||
| >>> nums | ||
| [0, 0, 1] | ||
|
|
||
| >>> nums = [-2, 1, 0] | ||
| >>> sleep_sort(nums) | ||
| >>> nums | ||
| [-2, 0, 1] | ||
|
|
||
| >>> sleep_sort([1.5, 2]) | ||
| Traceback (most recent call last): | ||
| ... | ||
| TypeError: integers only please | ||
| """ | ||
| if not numbers: | ||
| return | ||
|
|
||
| if any(not isinstance(x, int) for x in numbers): | ||
| raise TypeError("integers only please") | ||
|
|
||
| min_val = min(numbers) | ||
| offset = -min_val if min_val < 0 else 0 | ||
|
|
||
| if simulate: | ||
| # Simulated wake-up: bucket by wake time (value + offset), preserve order | ||
| buckets = {} | ||
| for idx, val in enumerate(numbers): | ||
| wake = val + offset | ||
| buckets.setdefault(wake, []).append((idx, val)) | ||
| result: List[int] = [] | ||
| for wake in sorted(buckets.keys()): | ||
| for _, val in buckets[wake]: | ||
| result.append(val) | ||
| numbers[:] = result | ||
| return | ||
|
|
||
| # Real threaded mode: causes actual delays proportional to element values | ||
| results: List[int] = [] | ||
| lock = threading.Lock() | ||
|
|
||
| def worker(value: int) -> None: | ||
|
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. As there is no test file in this pull request nor any test function or class in the file |
||
| time.sleep((value + offset) * scale) | ||
| with lock: | ||
| results.append(value) | ||
|
|
||
| threads: List[threading.Thread] = [] | ||
| for val in numbers: | ||
| t = threading.Thread(target=worker, args=(val,)) | ||
| t.daemon = True | ||
| t.start() | ||
| threads.append(t) | ||
|
|
||
| for t in threads: | ||
| t.join() | ||
|
|
||
| numbers[:] = results | ||
Oops, something went wrong.
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.
As there is no test file in this pull request nor any test function or class in the file
sorts/sleep_sort.py, please provide doctest for the functionworker