Skip to content

Instantly share code, notes, and snippets.

@yoppi
Created May 21, 2011 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoppi/984698 to your computer and use it in GitHub Desktop.
Save yoppi/984698 to your computer and use it in GitHub Desktop.
SleepSort
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