Skip to content
Closed
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
83 changes: 83 additions & 0 deletions sorts/sleep_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from typing import List

Check failure on line 1 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

sorts/sleep_sort.py:1:1: UP035 `typing.List` is deprecated, use `list` instead
import threading
import time

Check failure on line 3 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

sorts/sleep_sort.py:1:1: I001 Import block is un-sorted or un-formatted


def sleep_sort(numbers: List[int], simulate: bool = True, scale: float = 0.01) -> None:

Check failure on line 6 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/sleep_sort.py:6:25: UP006 Use `list` instead of `List` for type annotation
"""
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).

Check failure on line 15 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

sorts/sleep_sort.py:15:89: E501 Line too long (92 > 88)
- 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] = []

Check failure on line 57 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/sleep_sort.py:57:17: UP006 Use `list` instead of `List` for type annotation
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] = []

Check failure on line 65 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/sleep_sort.py:65:14: UP006 Use `list` instead of `List` for type annotation
lock = threading.Lock()

def worker(value: int) -> None:

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 worker

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 worker

time.sleep((value + offset) * scale)
with lock:
results.append(value)

threads: List[threading.Thread] = []

Check failure on line 73 in sorts/sleep_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/sleep_sort.py:73:14: UP006 Use `list` instead of `List` for type annotation
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
Loading