Skip to content

Instantly share code, notes, and snippets.

@ybur-yug
Last active August 29, 2015 14:13
Show Gist options
  • Save ybur-yug/d3399efb70b886ff87b8 to your computer and use it in GitHub Desktop.
Save ybur-yug/d3399efb70b886ff87b8 to your computer and use it in GitHub Desktop.
sleep sort, for the lulz
from time import sleep
from threading import Timer
# this is completely ridiculous and should never be used
def sleepsort(values):
sleepsort.result = []
def add_one(x):
sleepsort.result.append(x)
mx = values[0]
for v in values:
if mx < v: mx = v
Timer(v, add_one, [v]).start()
sleep(mx+1)
return sleepsort.result
if __name__ == '__main__':
x = [3,2,4,7,3,6,9,1]
if sleepsort(x) == sorted(x):
print('sleep sort worked for {0}'.format(x))
else:
print'sleep sort FAILED for {0}'.format(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment