Created
May 19, 2011 17:02
-
-
Save tmiz/981229 to your computer and use it in GitHub Desktop.
sleepsort.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sleep sort | |
import threading | |
import time | |
class SleepAndAppend(threading.Thread): | |
def __init__(self, value, targetList): | |
threading.Thread.__init__(self) | |
self.value = value | |
self.targetList = targetList | |
def run(self): | |
time.sleep(self.value * 0.01) | |
self.targetList.append(self.value) | |
def sleepSort(aList): | |
targetList = [] | |
threadList = [] | |
for a in aList: | |
th = SleepAndAppend(a, targetList) | |
th.start() | |
threadList.append(th) | |
for th in threadList: | |
th.join(); | |
return targetList | |
aList = [23,1,53,3,1,3,6,13,3,6,1,3,7,1,3,6,1,3,7,133,63,137] | |
print sleepSort(aList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
check this snippet using coruitine by miyagawa san
https://gist.github.com/982113