Created
June 21, 2011 12:53
-
-
Save hpcx82/1037793 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
--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