Created
May 21, 2011 17:16
-
-
Save yoppi/984698 to your computer and use it in GitHub Desktop.
SleepSort
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
require 'thread' | |
def sleep_sort(xs) | |
queue = Queue.new | |
xs.map {|x| | |
# 1000 is precision limit | |
Thread.new(x) {|_x| sleep(_x / 1000.0); queue.push(_x) } | |
}.each(&:join) | |
ret = [] | |
queue.size.times { | |
ret << queue.pop | |
} | |
ret | |
end | |
if __FILE__ == $0 | |
p sleep_sort([5, 3, 1, 4, 2]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment