Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vijayanandrp/5f40cc867f2ee8ce3e57e645110ece9c to your computer and use it in GitHub Desktop.
Save vijayanandrp/5f40cc867f2ee8ce3e57e645110ece9c to your computer and use it in GitHub Desktop.
Just 10 lines of code to implement interesting Sleep Sorting Algorithm using Python
#!/usr/bin/env python
from time import sleep
from threading import Timer
def sleep_sort(values):
sleep_sort.result = []
def add1(x):
print 'Appending value in list -> %d' % x
sleep_sort.result.append(x)
mx = values[0]
for value in values:
if mx < value: mx = value
print 'Calling Timer for value = %d' % value
Timer(value, add1, [value]).start()
sleep(mx+1)
return sleep_sort.result
if __name__ == '__main__':
x = [3,1,4, 1, 8, 5, 0]
if sleep_sort(x) == sorted(x):
print('Sleep sort worked for: ', x)
else:
print('Sleep sort FAILED for: ', x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment