Skip to content

Instantly share code, notes, and snippets.

@dyjjones
Created August 2, 2016 22:15
Show Gist options
  • Save dyjjones/6ecbdea47e1ee16618a8768afb73b8aa to your computer and use it in GitHub Desktop.
Save dyjjones/6ecbdea47e1ee16618a8768afb73b8aa to your computer and use it in GitHub Desktop.
sleep sort
from concurrent.futures import ThreadPoolExecutor
from time import sleep
from collections import Counter
from random import shuffle
from pprint import pprint
x = ThreadPoolExecutor(1000)
lst = list(range(10))
def sleepsort(l):
sorted_l = []
def handle_n(n):
sleep(n*.1)
sorted_l.append(n)
for num in x.map(handle_n, l):
pass
return sorted_l
results = []
for _ in range(100):
shuffle(lst)
results.append(tuple(sleepsort(lst)))
pprint(dict(Counter(results)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment