How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

In general, sleep sort works by starting a separate task for each item to be sorted, where each task sleeps for an interval corresponding to the item's sort key, then emits the item. Items are then collected sequentially in time. Task: Write a program that implements sleep sort. Have it accept non-negative integers on the command line and print the integers in sorted order. If this is not idomatic in your language or environment, input and output may be done differently. Enhancements for optimization, generalization, practicality, robustness, and so on are not required. Sleep sort was presented anonymously on 4chan and has been discussed on Hacker News.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Icon and Unicon programming language

Source code in the icon programming language

procedure main(A)
    every insert(t:=set(),mkThread(t,!A))
    every spawn(!t)    # start threads as closely grouped as possible
    while (*t > 0) do write(<<@)
end

procedure mkThread(t,n)    # 10ms delay scale factor
    return create (delay(n*10),delete(t,&current),n@>&main)
end


  

You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the Raku programming language
You may also check:How to resolve the algorithm Character codes step by step in the Zoea Visual programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the OCaml programming language
You may also check:How to resolve the algorithm Steffensen's method step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Icon and Unicon programming language