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

Published on 12 May 2024 09:40 PM

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

Source code in the factor programming language

USING: kernel math math.order math.vectors sequences ;
: fill ( seq len -- newseq ) [ dup length ] dip swap - 0 <repetition> append ;

: bead ( seq -- newseq )
dup 0 [ max ] reduce
[ swap 1 <repetition> swap fill ] curry map
[ ] [ v+ ] map-reduce ;

: beadsort ( seq -- newseq ) bead bead ;


( scratchpad ) { 5 2 4 1 3 3 9 } beadsort .
{ 9 5 4 3 3 2 1 }


  

You may also check:How to resolve the algorithm Count the coins step by step in the Julia programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Swift programming language
You may also check:How to resolve the algorithm A+B step by step in the Zoea Visual programming language
You may also check:How to resolve the algorithm Runge-Kutta method step by step in the APL programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Ada programming language