Skip to content

Commit 76694e4

Browse files
authored
Merge pull request #1 from ArvindAROO/ArvindAROO-patch-1
added sleepsort
2 parents f6ee518 + 7c2623f commit 76694e4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

sorts/sleepsort.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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)

0 commit comments

Comments
 (0)