Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions sorts/sleep_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import threading
import time


def sleep_sort(arr):

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 function sleep_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

"""
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):

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 function sleeper

Please provide return type hint for the function: sleeper. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

# 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)