Anonymous user
Sorting algorithms/Sleep sort: Difference between revisions
→{{header|NetRexx}}: Merge logic from two loops into one; make sure logic in run waits on latch
(→{{header|NetRexx}}: Merge logic from two loops into one; make sure logic in run waits on latch) |
|||
Line 422:
class RSortingSleepsort
properties constant private
dflt
properties indirect
startLatch = CountDownLatch
Line 437:
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method sleepSort(iArg) public
setFloor(getFloor().min(iArg.word(mn))) -- save smallest -ve number so we can use it as a scale▼
setStartLatch(CountDownLatch(1)) -- used to put all threads on hold until we're ready to run
setDoneLatch(CountDownLatch(iArg.words())) -- used to indicate all work is done
loop mn = 1 to iArg.words()
▲ setFloor(getFloor().min(iArg.word(mn))) -- save smallest -ve number so we can use it as a scale for sleep
Thread(SortThread(iArg.word(mn))).start() -- loop through input and create a thread for each element
end mn
Line 463 ⟶ 461:
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method run() public
sleepTime = getNum() + parent.getFloor().abs() -- shifted by value of smallest number (permits numbers < 0)▼
sleepTime = sleepTime * 250 -- scale up; milliseconds are not granular enough▼
do
parent.getStartLatch().await() -- wait until all threads are constructed
▲ sleepTime = getNum() + parent.getFloor().abs() -- shifted by value of smallest number (permits numbers < 0)
Thread.sleep(sleepTime) -- wait for this number's turn to run▼
▲ sleepTime = sleepTime * 250 -- scale up; milliseconds are not granular enough
▲ Thread.sleep(sleepTime) -- wait for this number's turn to run
catch ie = InterruptedException
ie.printStackTrace()
|