module Expertsort::SleepSort
Public Instance Methods
sleepsort()
click to toggle source
# File lib/expertsort/sorts/sleepsort.rb, line 3 def sleepsort sorted = [] threads = [] semaphore = Mutex.new # Reduce potential impact of thread creation time by joining them when all threads have been created self.each do |e| raise RangeError, "Cannot sleep sort an array with negative elements: #{e}" if e.to_i < 0 threads << Thread.new do sleep e.to_i semaphore.synchronize { sorted << e } end end threads.each { |t| t.join } sorted end
sleepsort!()
click to toggle source
# File lib/expertsort/sorts/sleepsort.rb, line 21 def sleepsort! self.replace(sleepsort) end