1- """
2- Sleep Sort Algorithm Implementation
3- """
4-
51import threading
62import time
73from typing import List
84
9-
105def sleep_sort (arr : List [int ]) -> List [int ]:
116 """
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
7+ Sleep sort implementation - each element sleeps for n seconds then gets appended
198 """
209 if not arr :
2110 return []
2211
2312 result = []
24- lock = threading .Lock ()
2513
26- def worker (value ):
27- time .sleep (value / 10 )
28- with lock :
29- result .append (value )
14+ def add_to_result (n ):
15+ time .sleep (n )
16+ result .append (n )
3017
3118 threads = []
32- for value in arr :
33- if value < 0 :
34- raise ValueError ("No negative numbers allowed" )
35- thread = threading .Thread (target = worker , args = (value ,))
36- threads .append (thread )
19+ for num in arr :
20+ thread = threading .Thread (target = add_to_result , args = (num ,))
3721 thread .start ()
22+ threads .append (thread )
3823
3924 for thread in threads :
4025 thread .join ()
4126
4227 return result
4328
44-
45- class SleepSort :
46- """Class-based sleep sort implementation."""
47-
48- def _init_ (self , speed_factor = 10.0 ):
49- self .speed_factor = speed_factor
50-
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- """
61- if not arr :
62- return []
63-
64- result = []
65- lock = threading .Lock ()
66-
67- def worker (value ):
68- time .sleep (value / self .speed_factor )
69- with lock :
70- result .append (value )
71-
72- threads = []
73- for value in arr :
74- if value < 0 :
75- raise ValueError ("No negative numbers allowed" )
76- thread = threading .Thread (target = worker , args = (value ,))
77- threads .append (thread )
78- thread .start ()
79-
80- for thread in threads :
81- thread .join ()
82-
83- return result
84-
85-
86- if __name__ == "_main_" :
87- # Test the algorithms
88- test_data = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 ]
89-
90- print ("Original array:" , test_data )
91-
92- # Test basic sleep sort
93- try :
94- sorted1 = sleep_sort (test_data )
95- print ("Basic sleep sort:" , sorted1 )
96- except Exception as e :
97- print ("Basic sleep sort error:" , e )
98-
99- # Test class-based sleep sort
100- try :
101- sorter = SleepSort (speed_factor = 20.0 )
102- sorted2 = sorter .sort (test_data )
103- print ("Class sleep sort:" , sorted2 )
104- except Exception as e :
105- print ("Class sleep sort error:" , e )
106-
107- print ("Algorithm completed successfully!" )
108-
29+ if __name__ == "__main__" :
30+ # Test the sleep sort
31+ user_input = input ("Enter numbers separated by commas: " )
32+ if user_input :
33+ numbers = [int (x ) for x in user_input .split ("," )]
34+ print ("Original:" , numbers )
35+ sorted_numbers = sleep_sort (numbers )
36+ print ("Sorted:" , sorted_numbers )
37+ else :
38+ print ("No input provided" )
0 commit comments