Skip to content

Commit 3fb4f0f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 8c6d8ce commit 3fb4f0f

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

sorts/sleep_sort.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,99 +10,98 @@
1010
def sleep_sort(arr: List[int]) -> List[int]:
1111
"""
1212
Sort list using sleep sort algorithm.
13-
13+
1414
Args:
1515
arr: List of non-negative integers
16-
16+
1717
Returns:
1818
Sorted list in ascending order
1919
"""
2020
if not arr:
2121
return []
22-
22+
2323
result = []
2424
lock = threading.Lock()
25-
25+
2626
def worker(value):
2727
time.sleep(value / 10)
2828
with lock:
2929
result.append(value)
30-
30+
3131
threads = []
3232
for value in arr:
3333
if value < 0:
3434
raise ValueError("No negative numbers allowed")
3535
thread = threading.Thread(target=worker, args=(value,))
3636
threads.append(thread)
3737
thread.start()
38-
38+
3939
for thread in threads:
4040
thread.join()
41-
41+
4242
return result
4343

4444

4545
class SleepSort:
4646
"""Class-based sleep sort implementation."""
47-
47+
4848
def _init_(self, speed_factor=10.0):
4949
self.speed_factor = speed_factor
50-
50+
5151
def sort(self, arr):
5252
"""
5353
Sort array using sleep sort.
54-
54+
5555
Args:
5656
arr: List of non-negative integers
57-
57+
5858
Returns:
5959
Sorted list
6060
"""
6161
if not arr:
6262
return []
63-
63+
6464
result = []
6565
lock = threading.Lock()
66-
66+
6767
def worker(value):
6868
time.sleep(value / self.speed_factor)
6969
with lock:
7070
result.append(value)
71-
71+
7272
threads = []
7373
for value in arr:
7474
if value < 0:
7575
raise ValueError("No negative numbers allowed")
7676
thread = threading.Thread(target=worker, args=(value,))
7777
threads.append(thread)
7878
thread.start()
79-
79+
8080
for thread in threads:
8181
thread.join()
82-
82+
8383
return result
8484

8585

8686
if __name__ == "_main_":
8787
# Test the algorithms
8888
test_data = [3, 1, 4, 1, 5, 9, 2, 6]
89-
89+
9090
print("Original array:", test_data)
91-
91+
9292
# Test basic sleep sort
9393
try:
9494
sorted1 = sleep_sort(test_data)
9595
print("Basic sleep sort:", sorted1)
9696
except Exception as e:
9797
print("Basic sleep sort error:", e)
98-
98+
9999
# Test class-based sleep sort
100100
try:
101101
sorter = SleepSort(speed_factor=20.0)
102102
sorted2 = sorter.sort(test_data)
103103
print("Class sleep sort:", sorted2)
104104
except Exception as e:
105105
print("Class sleep sort error:", e)
106-
106+
107107
print("Algorithm completed successfully!")
108-

0 commit comments

Comments
 (0)