-
-
Notifications
You must be signed in to change notification settings - Fork 48.6k
Add Sleep Sort algorithm to advanced Python sorting algorithms (#13203) #13415
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import threading | ||
import time | ||
|
||
|
||
def sleep_sort(arr): | ||
""" | ||
Sorts a list of positive integers using Sleep Sort. | ||
|
||
Args: | ||
arr (list[int]): List of positive integers to sort. | ||
|
||
Returns: | ||
list[int]: Sorted list in ascending order. | ||
""" | ||
result = [] | ||
|
||
def sleeper(x): | ||
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 Please provide return type hint for the function: Please provide descriptive name for the parameter: Please provide type hint for the parameter: |
||
# Sleep for a duration proportional to the number | ||
time.sleep(x * 0.01) # scale down to avoid long delays | ||
result.append(x) | ||
|
||
threads = [threading.Thread(target=sleeper, args=(num,)) for num in arr] | ||
|
||
# Start all threads | ||
for t in threads: | ||
t.start() | ||
|
||
# Wait for all threads to finish | ||
for t in threads: | ||
t.join() | ||
|
||
return result | ||
|
||
|
||
# Example Usage | ||
if __name__ == "__main__": | ||
numbers = [4, 1, 3, 2] | ||
sorted_numbers = sleep_sort(numbers) | ||
print("Original:", numbers) | ||
print("Sorted:", sorted_numbers) |
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 functionsleep_sort
Please provide return type hint for the function:
sleep_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:
arr