File tree 1 file changed +35
-0
lines changed
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Sleepsort is probably the wierdest of all sorting functions
3
+ with time-complexity of O(max(input)+n) which is
4
+ quite different from almost all other sorting techniques.
5
+ If the number of inputs is small then the complexity
6
+ can be approximated to be O(max(input)) which is a constant
7
+
8
+ If the number of inputs is large then the complexity is
9
+ approximately O(n)
10
+
11
+ This function uses multithreading a kind of higher order programming
12
+ and calls n functions, each with a sleep time equal to its number.
13
+ Hence each of the functions wake in Sorted form
14
+
15
+ But this function is not stable for very large values
16
+ """
17
+ from time import sleep
18
+ from threading import Timer
19
+
20
+ def sleepsort (values ):
21
+ sleepsort .result = []
22
+ def add1 (x ):
23
+ sleepsort .result .append (x )
24
+ mx = values [0 ]
25
+ for v in values :
26
+ if mx < v :
27
+ mx = v
28
+ Timer (v , add1 , [v ]).start ()
29
+ sleep (mx + 1 )
30
+ return sleepsort .result
31
+
32
+ if __name__ == '__main__' :
33
+ x = [3 ,2 ,4 ,7 ,3 ,6 ,9 ,1 ]
34
+ sorted_x = sleepsort (x )
35
+ print (sorted_x )
You can’t perform that action at this time.
0 commit comments