How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the jq 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 jq programming language

Source code in the jq programming language

echo '[5, 1, 3, 2, 11, 6, 4]' | jq '
def f:
  if .unsorted == [] then
    .sorted
  else
    { unsorted: [.unsorted[] | .t = .t - 1 | select(.t != 0)]
    , sorted: (.sorted + [.unsorted[] | .t = .t - 1 | select(.t == 0) | .v]) }
    | f
  end;
{unsorted: [.[] | {v: ., t: .}], sorted: []} | f | .[]
'

  

You may also check:How to resolve the algorithm String concatenation step by step in the Go programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Nim programming language
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the BASIC256 programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm FASTA format step by step in the Raku programming language