How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Raku programming language

Table of Contents

Problem Statement

Sort an array of positive integers using the Bead Sort Algorithm. A   bead sort   is also known as a   gravity sort.

Algorithm has   O(S),   where   S   is the sum of the integers in the input set:   Each bead is moved individually. This is the case when bead sort is implemented without a mechanism to assist in finding empty spaces below the beads, such as in software implementations.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Raku programming language

Source code in the raku programming language

# routine cribbed from List::Utils;
sub transpose(@list is copy) {
    gather {
        while @list {
            my @heads;
            if @list[0] !~~ Positional { @heads = @list.shift; }
            else { @heads = @list.map({$_.shift unless $_ ~~ []}); }
            @list = @list.map({$_ unless $_ ~~ []});
            take [@heads];
        }
    }
}

sub beadsort(@l) {
    (transpose(transpose(map {[1 xx $_]}, @l))).map(*.elems);
}

my @list = 2,1,3,5;
say beadsort(@list).perl;


sub beadsort(*@list) {
    my @rods;
    for words ^«@list -> $x { @rods[$x].push(1) }
    gather for ^@rods[0] -> $y {
        take [+] @rods.map: { .[$y] // last }
    }
}

say beadsort 2,1,3,5;


  

You may also check:How to resolve the algorithm Sort an integer array step by step in the REXX programming language
You may also check:How to resolve the algorithm Sockets step by step in the Slate programming language
You may also check:How to resolve the algorithm Fermat numbers step by step in the Factor programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Unbias a random generator step by step in the Rust programming language