Skip to content

Commit 8c6d8ce

Browse files
feat: add sleep sort algorithm
2 parents 7a5be70 + 7582e72 commit 8c6d8ce

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

sorts/bubble_sort.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

sorts/sleep_sort.py

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,108 @@
1-
2-
"""Sleep Sort Algorithm."""
1+
"""
2+
Sleep Sort Algorithm Implementation
3+
"""
34

45
import threading
56
import time
67
from typing import List
78

9+
810
def sleep_sort(arr: List[int]) -> List[int]:
9-
"""Sort list using sleep sort."""
11+
"""
12+
Sort list using sleep sort algorithm.
13+
14+
Args:
15+
arr: List of non-negative integers
16+
17+
Returns:
18+
Sorted list in ascending order
19+
"""
1020
if not arr:
1121
return []
12-
result: List[int] = []
22+
23+
result = []
1324
lock = threading.Lock()
1425

15-
def worker(value: int) -> None:
26+
def worker(value):
1627
time.sleep(value / 10)
1728
with lock:
1829
result.append(value)
1930

20-
threads: List[threading.Thread] = []
31+
threads = []
2132
for value in arr:
2233
if value < 0:
23-
raise ValueError("No negative numbers")
34+
raise ValueError("No negative numbers allowed")
2435
thread = threading.Thread(target=worker, args=(value,))
2536
threads.append(thread)
2637
thread.start()
2738

2839
for thread in threads:
2940
thread.join()
41+
3042
return result
3143

32-
def sleep_sort_simple(arr: List[int]) -> List[int]:
33-
"""Simple non-threaded version."""
34-
if not arr:
35-
return []
36-
return sorted(arr)
3744

3845
class SleepSort:
3946
"""Class-based sleep sort implementation."""
4047

41-
def _init_(self, speed_factor: float = 10.0) -> None:
48+
def _init_(self, speed_factor=10.0):
4249
self.speed_factor = speed_factor
4350

44-
def sort(self, arr: List[int]) -> List[int]:
51+
def sort(self, arr):
52+
"""
53+
Sort array using sleep sort.
54+
55+
Args:
56+
arr: List of non-negative integers
57+
58+
Returns:
59+
Sorted list
60+
"""
4561
if not arr:
4662
return []
47-
result: List[int] = []
63+
64+
result = []
4865
lock = threading.Lock()
4966

50-
def worker(value: int) -> None:
67+
def worker(value):
5168
time.sleep(value / self.speed_factor)
5269
with lock:
5370
result.append(value)
5471

55-
threads: List[threading.Thread] = []
72+
threads = []
5673
for value in arr:
5774
if value < 0:
58-
raise ValueError("No negative numbers")
75+
raise ValueError("No negative numbers allowed")
5976
thread = threading.Thread(target=worker, args=(value,))
6077
threads.append(thread)
6178
thread.start()
6279

6380
for thread in threads:
6481
thread.join()
82+
6583
return result
6684

85+
6786
if __name__ == "_main_":
68-
test_arr = [3, 1, 4, 1, 5, 9, 2, 6]
69-
print("Original array:", test_arr)
70-
print("Simple sorted:", sleep_sort_simple(test_arr))
87+
# Test the algorithms
88+
test_data = [3, 1, 4, 1, 5, 9, 2, 6]
89+
90+
print("Original array:", test_data)
7191

92+
# Test basic sleep sort
7293
try:
73-
threaded = sleep_sort(test_arr)
74-
print("Threaded sorted:", threaded)
94+
sorted1 = sleep_sort(test_data)
95+
print("Basic sleep sort:", sorted1)
7596
except Exception as e:
76-
print("Threaded error:", e)
97+
print("Basic sleep sort error:", e)
7798

99+
# Test class-based sleep sort
78100
try:
79101
sorter = SleepSort(speed_factor=20.0)
80-
class_sorted = sorter.sort(test_arr)
81-
print("Class sorted:", class_sorted)
102+
sorted2 = sorter.sort(test_data)
103+
print("Class sleep sort:", sorted2)
82104
except Exception as e:
83-
print("Class error:", e)
105+
print("Class sleep sort error:", e)
106+
107+
print("Algorithm completed successfully!")
108+

0 commit comments

Comments
 (0)