Skip to content

Instantly share code, notes, and snippets.

@hpcx82
Created June 21, 2011 12:53
Show Gist options
  • Save hpcx82/1037793 to your computer and use it in GitHub Desktop.
Save hpcx82/1037793 to your computer and use it in GitHub Desktop.
--sleep sort
function sleepsort(arr)
local res, thread = {}, {}
local nthreads = #arr
for i = 1, #arr do
thread[i] = coroutine.create(function()
for n=arr[i], 0, -1 do coroutine.yield() end
nthreads = nthreads - 1
thread[i] = nil
res[#res+1] = arr[i]
end)
end
while nthreads > 0 do
for i = 1, #arr do
if thread[i] then coroutine.resume(thread[i]) end
end
end
return res
end
math.randomseed(os.time())
local arr = {}
for i = 1,10 do arr[i] = math.random(1,99) end
print(unpack(sleepsort(arr)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment