Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@
* [Doppler Frequency](physics/doppler_frequency.py)
* [Escape Velocity](physics/escape_velocity.py)
* [Grahams Law](physics/grahams_law.py)
* [Hamiltonian](physics/hamiltonian.py)
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
* [Hubble Parameter](physics/hubble_parameter.py)
* [Ideal Gas Law](physics/ideal_gas_law.py)
Expand Down Expand Up @@ -1404,6 +1405,7 @@
* [Selection Sort](sorts/selection_sort.py)
* [Shell Sort](sorts/shell_sort.py)
* [Shrink Shell Sort](sorts/shrink_shell_sort.py)
* [Sleep Sort](sorts/sleep_sort.py)
* [Slowsort](sorts/slowsort.py)
* [Smoothsort](sorts/smoothsort.py)
* [Stalin Sort](sorts/stalin_sort.py)
Expand Down
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):

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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 descriptive name for the parameter: x

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 type hint for the parameter: x

Copy link
Copy Markdown

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 descriptive name for the parameter: x

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